Not all arguments converted during string formatting (Python)

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Johan Mouchet

Not all arguments converted during string formatting (Python) #

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.

typeerror not all arguments converted during string formatting

Here is an example of how to use the str.format() method.

main.py
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.

main.py
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.

main.py
my_str = '99' # ✅ convert to int result = int(my_str) % 5 print(result) # 👉️ 4
IMPORTANT: if you use the input() built-in function, all of the values the user enters get converted to strings (even numeric values).

Once the values on both sides of the % (modulo) operator are integers, the error will be resolved.

If you got the error while formatting a string, make sure that each replacement field contains either the numeric index of a position argument or the name of a keyword argument.

main.py
first = 'James' last = 'Doe' result = "His name is {f} {l}".format(f=first, l=last) print(result) # 👉️ "His name is James Doe"

The example above uses keyword arguments instead of position ones.

Make sure to provide exactly as many arguments to the 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.

Alternatively, you can use a formatted string literal to concatenate strings.

main.py
first = 'James' last = 'Doe' result = f'His name is {first} {last}' print(result) # 👉️ "His name is James Doe"
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with 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.

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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.

Use the search field on my Home Page to filter through my more than 1,000 articles.