Such a common error is TypeError: not all arguments converted during string formatting. This error is caused when there is a mismatch in data types and strings are not properly formatted. The solution to this error is to use proper string formatting functions such as int() or str() to obtain the desired data type.
The “not all arguments converted during string formatting” error is raised when Python does not add in all arguments to a string format operation. This happens if you mix up your string formatting syntax or if you try to perform a modulo operation on a string.
%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.
They are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number.
You're mixing different format functions.
The old-style %
formatting uses %
codes for formatting:
'It will cost $%d dollars.' % 95
The new-style {}
formatting uses {}
codes and the .format
method
'It will cost ${0} dollars.'.format(95)
Note that with old-style formatting, you have to specify multiple arguments using a tuple:
'%d days and %d nights' % (40, 40)
In your case, since you're using {}
format specifiers, use .format
:
"'{0}' is longer than '{1}'".format(name1, name2)
The error is in your string formatting.
The correct way to use traditional string formatting using the '%' operator is to use a printf-style format string (Python documentation for this here: http://docs.python.org/2/library/string.html#format-string-syntax):
"'%s' is longer than '%s'" % (name1, name2)
However, the '%' operator will probably be deprecated in the future. The new PEP 3101 way of doing things is like this:
"'{0}' is longer than '{1}'".format(name1, name2)
For me, This error was caused when I was attempting to pass in a tuple into the string format method.
I found the solution from this question/answer
Copying and pasting the correct answer from the link (NOT MY WORK):
>>> thetuple = (1, 2, 3)
>>> print "this is a tuple: %s" % (thetuple,)
this is a tuple: (1, 2, 3)
Making a singleton tuple with the tuple of interest as the only item, i.e. the (thetuple,) part, is the key bit here.
In my case, it's because I need only a single %s
, i missing values input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With