Not all arguments converted during string formatting (Python)

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# 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
# โœ… Using str.format() correctly first = 'James' last = 'Doe' result = "His name is {0} {1}".format(first, last) print(result) # ๐Ÿ‘‰๏ธ "His name is James Doe"

using str format method correctly

The str.format() method performs string formatting operations.

The string the method is called on can contain replacement fields specified using curly braces {}.

# Using the modulo operator with a string and an integer

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)

using modulo operator with string and int

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

converting the string to an int

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.

# The input() function always returns a string

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.

main.py
# โœ… Convert a value to an integer with int() num = int(input('Enter your fav number: ')) print(num) # ๐Ÿ‘‰๏ธ 10 print(type(num)) # ๐Ÿ‘‰๏ธ <class 'int'>

# Using the str.format() method correctly

If 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.

main.py
first = 'James' last = 'Doe' result = "His name is {f} {l}".format(f=first, l=last) print(result) # ๐Ÿ‘‰๏ธ "His name is James Doe"

using str format with keyword arguments

The example uses keyword arguments instead of positional 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.

main.py
first = 'James' last = 'Doe' result = "His name is {0} {1}".format(first, last) print(result) # ๐Ÿ‘‰๏ธ "His name is James Doe"

# Using a formatted string literal to solve the error

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

main.py
first = 'Bobby' last = 'Hadz' result = f'His name is {first} {last}' print(result) # ๐Ÿ‘‰๏ธ "His name is Bobby Hadz"

using formatted string literal

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.

Formatted string literals also take care of converting the value to a string automatically.

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

main.py
name = 'Bobby' age = 30 result = name + ' is ' + str(age) + ' years old.' print(result) # ๐Ÿ‘‰๏ธ "Bobby is 30 years old."

# Checking what type the variable stores

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.

I've also written an article on how to use f-strings for conditional formatting.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev