How to do [1]*4 for a tuple

V

Vineet Jain

I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when I
have to do the following:

'%s some value %s and some other text %s' % (a)*3

as apposed to

'%s some value %s and some other text %s' % (a, a, a)

In this case I always end up doing

'%s some value %s and some other text %s' % [a]*3

VJ
 
R

Rob Williscroft

Vineet Jain wrote in (e-mail address removed):
I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when
I have to do the following:

'%s some value %s and some other text %s' % (a)*3
'<aaa> some value <aaa> and some other text <aaa>'


as apposed to

'%s some value %s and some other text %s' % (a, a, a)

In this case I always end up doing

'%s some value %s and some other text %s' % [a]*3

tuples are comma seperated items, parenthesis just bracket:

Rob.
 
S

Stephen Horne

I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when I
have to do the following:

'%s some value %s and some other text %s' % (a)*3

as apposed to

'%s some value %s and some other text %s' % (a, a, a)

In this case I always end up doing

'%s some value %s and some other text %s' % [a]*3

You have the answer to your question already, but you may be better
off using a dictionary with the % operator to get named association...
aaa some value aaa and some other text aaa

This works particularly well when not all substituted strings are the
same, and when you may want to change format strings in such a way
that the substitutions may not be in the same order, or may not all be
used, etc. In other words, it can be very useful when the format
strings are supplied externally.

It can be handy when the support guys want to decide the wording of
error messages (and change their minds every other day) for instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top