Last updated: Apr 8, 2024
Reading timeยท3 min
The Python "TypeError: not all arguments converted during string formatting"
occurs when we use incorrect syntax to format a string or use the %
operator
with a string and a number.
To solve the error, call the format()
method on the string and provide
values for all placeholders.
Here is an example of how to use the str.format()
method.
# โ Using str.format() correctly first = 'James' last = 'Doe' result = "His name is {0} {1}".format(first, last) print(result) # ๐๏ธ "His name is James Doe"
The str.format() method performs string formatting operations.
The string the method is called on can contain replacement fields specified
using curly braces {}
.
The error also occurs when you try to use the %
(modulo) operator with a
string and an integer.
my_str = '99' result = my_str % 5 # โ๏ธ TypeError: not all arguments converted during string formatting print(result)
To solve the error, use the int() class to convert the string to an integer.
my_str = '99' # โ convert to int result = int(my_str) % 5 print(result) # ๐๏ธ 4
We used the int()
class to convert the string to an integer to be able to use
the modulo %
operator.
Once the values on both sides of the %
(modulo) operator are integers, the
error will be resolved.
Note that the input()
function always returns a string, even if the user
enters an integer.
If you take input from the user, make sure to use the int()
class to convert
the value to an integer.
# โ Convert a value to an integer with int() num = int(input('Enter your fav number: ')) print(num) # ๐๏ธ 10 print(type(num)) # ๐๏ธ <class 'int'>
str.format()
method correctlyIf you get the error while formatting a string, make sure that each replacement field contains either the numeric index of a positional argument or the name of a keyword argument.
first = 'James' last = 'Doe' result = "His name is {f} {l}".format(f=first, l=last) print(result) # ๐๏ธ "His name is James Doe"
The example uses keyword arguments instead of positional ones.
format()
method as you have replacement fields in the string.If you decide to use positional arguments with numeric indices, make sure to
start from 0
, e.g. {0}
as indices are zero-based in Python.
first = 'James' last = 'Doe' result = "His name is {0} {1}".format(first, last) print(result) # ๐๏ธ "His name is James Doe"
Alternatively, you can use a formatted string literal to concatenate strings.
first = 'Bobby' last = 'Hadz' result = f'His name is {first} {last}' print(result) # ๐๏ธ "His name is Bobby Hadz"
f
.Make sure to wrap expressions in curly braces - {expression}
.
This should be your preferred approach as formatted string literals are a more recent addition to Python and offer a much easier-to-read syntax.
Formatted string literals also take care of converting the value to a string automatically.
name = 'Bobby' age = 30 result = f'{name} is {age} years old.' print(result) # ๐๏ธ "Bobby is 30 years old."
We didn't have to convert the integer to a string which would have been the case had we used the addition (+) operator.
name = 'Bobby' age = 30 result = name + ' is ' + str(age) + ' years old.' print(result) # ๐๏ธ "Bobby is 30 years old."
If you aren't sure what type a variable stores, use the built-in type()
class.
my_str = '99' print(type(my_str)) # ๐๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐๏ธ True my_num = 5 print(type(my_num)) # ๐๏ธ <class 'int'> print(isinstance(my_num, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
I've also written an article on how to use f-strings for conditional formatting.
You can learn more about the related topics by checking out the following tutorials: