Problem with string format

John Machin sjmachin at lexicon.net
Wed Mar 11 06:24:35 CET 2009


On Mar 11, 4:07 pm, Mike314 <michae... at gmail.com> wrote:
> Hello,
>
>    I have a strange problem with the string format:
>
> >>> '%s %s %s %s %s' % ['01', '02', '03', '04', '05']
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: not enough arguments for format string
>
> But as soon I use tuple it is working:>>> '%s %s %s %s %s' % ('01', '02', '03', '04', '05')
>
> '01 02 03 04 05'
>
> What is the problem

Docs:
"""
Given format % values (where format is a string or Unicode object), %
conversion specifications in format are replaced with zero or more
elements of values. [snip]

If format requires a single argument, values may be a single non-tuple
object. [snip] Otherwise, values must be a tuple with exactly the
number of items specified by the format string, or a single mapping
object (for example, a dictionary).
"""

> and how can I still use list?
# so you obtain a list somehow
alist = ['01', '02', '03', '04', '05']
# and much later you need to format the contents
formatted = '%s %s %s %s %s' % tuple(alist)



More information about the Python-list mailing list