Python TypeError: not all arguments converted during string formatting [Solved]

For example:

 strs=(1,2,3,4)  #Create a collection
 strs
 (1, 2, 3,4)
 >>> print 'strs= %s ' % strs
 Traceback (most recent call last):
   File "<pyshell#43>", line 1, in <module>
     print 'strs= %s ' % str
 TypeError: not all arguments converted during string formatting

Reason: the 1% operator can only be used directly for string (‘123 ‘). If it is a list ([1,2,3]) or tuple, it needs to match operators one by one.

To put it bluntly, it’s written in parentheses with commas (STRs,)

Solution:

print 'strs= %s' % (strs,)
strs= (1, 2, 3,4)
or
print 'strs= %s,%s,%s,%s' % sstr
strs= 1,2,3,4 

# simple explanation
the% before and after explanation does not correspond to the number of parameters after explanation, such as

File "<pyshell#37>", line 1, in <module>
print '%f meters is the same as &f km' % (meters, kilometers)
TypeError: not all arguments converted during string formatting

There are two parameters of miles and kilometer in the back, only one% F in the front, and one & amp; with wrong print;, The former is inconsistent with the latter; If you change it to

print '%f miles is the same as %f km' % (miles, kilometers)

That’s all


Read More: