0

I am trying to place several values from a list into a string. The code I have is below:

ID = [0, 1, 2]
print 'ID {0}, {1}, and {2}.'.format(ID)

or

print (r'(ID\s*=\s*)(\S+)').format(ID)

This does not work. Does anyone know where I'm going wrong. The code in the second line prints out the list:

[0, 1, 2]

the first line says:

File "tset.py", line 39, in b
    print 'ID {0}, {1}, and {2}.'.format(ID)
IndexError: tuple index out of range

Thanks

2
  • code doesn't work is not an explanation. do you have any error? Commented Jan 8, 2010 at 12:05
  • 1
    oh, dear. Look, your second addition has almost no relation to the question originally asked. Please remove it and ask a new question. And you'll need to include proper formatted, full code and not some random pieces. Commented Jan 8, 2010 at 12:23

2 Answers 2

9

You have to unpack the list.

ID = [0, 1, 2]
print 'ID {0}, {1}, and {2}.'.format(*ID)

See the docs: Unpacking argument lists.

Sign up to request clarification or add additional context in comments.

Comments

4
>>> 'ID {0}, {1}, and {2}.'.format(*ID)
'ID 0, 1, and 2.'

You need to unpack your list.

Your second code doesn't make much sense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.