Problem with string format

M

Mike314

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:'01 02 03 04 05'


What is the problem and how can I still use list?

Thanks.
 
S

Steven D'Aprano

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:'01 02 03 04 05'


What is the problem and how can I still use list?

There is no problem and you can't. As far as string formatting is
concerned, a list is a single object which goes into a single %s.


Try converting the list into a tuple first:

'%s %s %s %s %s' % tuple(['01', '02', '03', '04', '05'])
 
M

Mensanator

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 and how can I still use list?
'%s %s %s %s %s' % tuple(['01', '02', '03', '04', '05'])
'01 02 03 04 05'
 
J

John Machin

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)
 
T

Terry Reedy

Mike314 said:
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:'01 02 03 04 05'


What is the problem and how can I still use list?

This sort of confusion between object as object and object as container
of objects was one of the reasons for the development of the new
str.format function. Within a call, [1,2,3] is one argument matching
one field. *[1,2,3], for instance, becomes three arguments matching
three fields.
 

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

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,294
Latest member
HollieYork

Latest Threads

Top