If you are formatting a string in Python using the % operator, there are a set of rules you must stick to; otherwise, you will raise the error TypeError: not all arguments converted during string formatting.
This tutorial will go through the various ways this error can occur and how to solve it with the help of code examples.
Table of contents
- TypeError: not all arguments converted during string formatting
- Example #1: Incorrectly Using the Format Specifier in Place of Modulo Operation
- Example #2: Not Using the Format Specifier in the String
- Example #3: Incorrect Number of Format Specifiers
- Example #4: Mixing Different Format Functions
- Summary
TypeError: not all arguments converted during string formatting
What is a TypeError?
TypeError tells us that we are trying to perform an illegal operation for a specific Python data type.
What is String Formatting in Python?
String formatting is the process of dynamically implanting values in a string and presenting a new string. There are four ways to perform string formatting in Python:
- Formatting with % operator
- Formatting with the built-in format() string method
- Formatting with string literals, called f-strings
- Formatting using the template class from the string module
We can use the % operator to infuse a set of variables enclosed in a tuple into a format string. The format string contains text with argument specifiers. For example, %s specifies a string argument and %d specifies an integer argument. Let’s look at an example of string formatting with the % operator.
Example #1: Incorrectly Using the Format Specifier in Place of Modulo Operation
Let’s look at an example where we take a number provider by the user and determine whether the number 2 is a factor.
number = input("Enter a number: ")
modulo_of_number = number % 2
if modulo_of_number == 0:
print(f'2 is a factor of {number}')
else:
print(f'(2 is not a factor of {number}')
In the above code, we use the input() function to get the input from the user and then use the modulo operator to get the remainder of when we divide the number by 2. If the remainder is zero, we print that 2 is a factor of the input number. Otherwise, we print that 2 is not a factor. Let’s run the code to see the result:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
1 number = input("Enter a number: ")
2
----≻ 3 modulo_of_number = number % 2
4
5 if modulo_of_number == 0:
TypeError: not all arguments converted during string formatting
We raise the error because the number is not an integer. The input() function returns a string. Therefore if we use the % operator, Python interprets it as a string formatting operator.
Solution
We need to convert the input to a number using the int() method to solve the error. Let’s look at the revised code:
number = int(input("Enter a number: "))
modulo_of_number = number % 2
if modulo_of_number == 0:
print(f'2 is a factor of {number}')
else:
print(f'(2 is not a factor of {number}')
Let’s run the code to see the result:
Enter a number: 4
2 is a factor of 4
Python successfully interprets the number as an integer, not a string.
Example #2: Not Using the Format Specifier in the String
Let’s look at an example where we interpolate a number into a string using string formatting.
a = "An example of a prime number is "
b = 17
print(a % b)
Let’s run the code to see the result:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
----≻ 1 print(a % b)
TypeError: not all arguments converted during string formatting
The error occurs because we did not use a format specifier in the string.
Solution
To solve this error, we need to put a format specifier in the string with the conversion we want to do before interpolating the number. We will use %s to convert the value to a Unicode string. Let’s look at the revised code:
a = "An example of a prime number is %s"
b = 17
print(a % b)
Let’s look at the revised code:
An example of a prime number is 17
Example #3: Incorrect Number of Format Specifiers
Let’s look at an example where we take three values from the user to format a string.
name = input("Enter a name: ")
age = int(input("Enter an age: "))
country = input("Enter a country: ")
print("Details are name: %s age: %s country" % (name, age, country))
Let’s run the code to see what happens:
Enter a name: Jim
Enter an age: 20
Enter a country: Mali
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
5 country = input("Enter a country: ")
6
----≻ 7 print("Details are name: %s age: %s country" % (name, age, country))
TypeError: not all arguments converted during string formatting
The error occurs because we only have two format specifiers, whereas there are three arguments we want to convert.
Solution
To solve this error, we need to use the same number of format specifiers as arguments. Let’s look at the revised code:
name = input("Enter a name: ")
age = int(input("Enter an age: "))
country = input("Enter a country: ")
print("Details are name: %s age: %s country: %s" % (name, age, country))
Let’s run the code to see the result.
Enter a name: Jim
Enter an age: 20
Enter a country: Mali
Details are name: Jim age: 20 country: Mali
Example #4: Mixing Different Format Functions
Let’s look at an example where we attempt to interpolate two values into a string.
name = input("Enter a name: ")
age = int(input("Enter an age: "))
print("Details are name: {0} age: {1} " % name, age)
Let’s run the code to see what happens:
Enter a name: Tom
Enter an age: 55
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
3 age = int(input("Enter an age: "))
4
----≻ 5 print("Details are name: {0} age: {1} " % name, age)
TypeError: not all arguments converted during string formatting
The error occurs because we use the specifiers for the format() method and the formatting operator %. We have to use one string formatting method and its syntax.
Solution #1: Use format() method
We can use the format syntax, which differs from the % string formatting syntax. We need to use the curly brackets {} as placeholders for our arguments in the format string and then call the format() method on the string with the arguments. Let’s look at the revised code:
name = input("Enter a name: ")
age = int(input("Enter an age: "))
print("Details are name: {0} age: {1} ".format(name, age))
Let’s run the code to see the result:
Enter a name: Tom
Enter an age: 55
Details are name: Tom age: 55
Solution #2: Use f-string
As of Python 3.6, you can use the string formatting method called literal string interpolation or f-strings. The method is a more straightforward approach to string formatting. To create an f-string, you need to prefix the string with the letter f. The syntax still requires curly brackets, but you place the variables inside the curly brackets. Let’s look at the revised code:
name = input("Enter a name: ")
age = int(input("Enter an age: "))
print(f'Details are name: {name} age: {age}')
Let’s run the code to see the result:
Enter a name: Tom
Enter an age: 55
Details are name: Tom age: 55
Solution #3: Use % Operator
You can also use the % formatting method and provide the arguments in a tuple. We replace the curly brackets {} with the format specifier %s for each argument. Let’s look at the revised code:
name = input("Enter a name: ")
age = int(input("Enter an age: "))
print("Details are name: %s age: %s " % (name, age))
Let’s run the code to see the result:
Enter a name: Tom
Enter an age: 55
Details are name: Tom age: 55
Summary
Congratulations on reading to the end of this tutorial! The error TypeError: not all arguments converted during string formatting occurs if you do not include all arguments during a string formatting operation. This commonly happens when you use incorrect string formatting syntax, you do not use the correct number of format specifiers or if you try to perform a modulo operation on a string.
For further reading on TypeErrors involving string formatting, go to the article: How to Solve Python TypeError: not enough arguments for format string.
Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.