TypeError: not all arguments converted during string formatting python
TypeError: not all arguments converted during string formatting python
Youre 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 youre 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)
TypeError: not all arguments converted during string formatting python
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.