The TypeError: not all arguments converted during string formatting error occurs if no format specifier is available or the number of format specifiers is less than the number of values passed. The error is caused when Python does not convert all arguments to the string format operation. Also, this python error TypeError: not all arguments converted during string formatting occurs when you are mixing different format specifiers. The percentage % formatting uses % codes. The new-style {} formatting uses {} codes and the format() method.
The string format specifyer is used to dynamically replace the value in the string. The string uses percent (“%”) to specify the formats. The new string format specifyer uses the curly braces. The number of format specified and the number of values passed should be same.
If the new and old string formats are mixed, the python interpreter will throw this error “TypeError: not all arguments converted during string formatting. If both are mixed, python interpreter can not match the format specifiers with the values passed in the string.
Exception
The type error stack trace will be shown as below
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print x % y
TypeError: not all arguments converted during string formatting
[Finished in 0.1s with exit code 1]
Root Cause
This type error is due to the following reasons.
- There is no format specifier.
- The number of format specifier is less than the number of values passed
- The two string format specifiers are mixed.
Solution 1
If the string does not have format specifyer and contains a number, then the percent “%” is used as a mod operation. The mathematical mod will be performed with two numbers and the remainder will be returned. The string format specifier will not apply in this case. The error “TypeError: not all arguments converted during string formatting” will be resolved on this.
Program
x = "10"
y = 5
print x % y
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print x % y
TypeError: not all arguments converted during string formatting
[Finished in 0.1s with exit code 1]
Solution
x = "10"
y = 5
print int(x) % y
Output
0
[Finished in 0.1s]
Solution 2
The string does not have any format specifier and the value is passed to be replaced, so the python interpreter can not replace the value. The format specifier must be added in the string. The python interpreter will match the format specifier with the passed values. So, this will resolve the type error.
Program
x = "Your cost is "
y = 5
print x % y
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print x % y
TypeError: not all arguments converted during string formatting
[Finished in 0.0s with exit code 1]
Solution
x = "Your cost is %s"
y = 5
print x % y
Output
Your cost is 5
[Finished in 0.0s]
Solution 3
In python, if the number of format specifier is less than the value passed, the python interpreter can not replace all the values in the string format. The missing format specifier must be added in the string. This will resolve the type error.
Program
x = 5
y = 10
print "The values are %s" % (x, y)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print "The values are %s" % (x, y)
TypeError: not all arguments converted during string formatting
[Finished in 0.1s with exit code 1]
Solution
x = 5
y = 10
print "The values are %s %s" % (x, y)
Output
The values are 5 10
[Finished in 0.0s]
Solution 4
In python, if the two string formats are mixed in the string, the python interpreter can not replace all the values in the string format. The string should be followed by any one of the format specifiers to replace values. Convert the string to a single format. This is going to solve the type error.
Program
x = 5
y = 10
print "The values are {0} {1}" % (x, y)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print "The values are {0} {1}" % (x, y)
TypeError: not all arguments converted during string formatting
[Finished in 0.0s with exit code 1]
Solution
x = 5
y = 10
print "The values are %s %s" % (x, y)
Output
The values are 5 10
[Finished in 0.0s]
Solution 5
In python, if the two string formats are mixed in the string, the python interpreter can not replace all the values in the string format. The string should be followed by any one of the format specifiers to replace values. Convert the string to a single format. This is going to solve the type error.
Program
x = 5
y = 10
print "The values are {0} {1}" % (x, y)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print "The values are {0} {1}" % (x, y)
TypeError: not all arguments converted during string formatting
[Finished in 0.0s with exit code 1]
Solution
x = 5
y = 10
print "The values are {0} {1}".format(x, y)
Output
The values are 5 10
[Finished in 0.0s]