1

Example:

a = ( 1, 2, 3, 4 )
print( 'Test %s' % (a) ) # this will generate error not all arguments converted during string formatting
print( 'Test %s' % (a,) ) # this will print 'Test (1,2,3,4)'

Does this mean that it will just convert the whole list (including the brackets) to a string?

I just would like to confirm the above question.

  • 3
    That is not a list, it is a tuple. And yes, it treats the tuple itself as the object to interpolate into the string, and the tuple representation includes the brackets. Finally, it's worth noting, this is a rather old style, and you should consider using `.format` or f-strings instead. This is one of the biggest problems with if for me, actually, that there is a special case for tuples specifically. – juanpa.arrivillaga Jun 23 '18 at 19:07
  • looks like a duplicate of : https://stackoverflow.com/questions/1455602/printing-tuple-with-string-formatting-in-python – koalaok Jun 23 '18 at 19:10
  • 1
    This is one of the reasons to stop using old-style printf formatting and switch to using [format strings](https://docs.python.org/3/library/string.html#formatstrings) or (when using Python 3.6+ and you are formatting in-situ using literals) [formatted string literals](https://docs.python.org/3/reference/lexical_analysis.html#f-strings). – Martijn Pieters Jun 23 '18 at 19:23

0 Answers0