The error TypeError: not enough arguments for format string occurs if the number of arguments you specify during string formatting is not equal to the number of values you want to add to the string. The error can also occur if you forget to enclose the values for string formatting in parentheses.
This tutorial will go through how to solve this error with code examples.
Table of contents
TypeError: not enough arguments for format string
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.
name = "Paul"
age = 40
print("%s is %d years old." %(name, age))
Paul is 40 years old.
If you do not use parentheses to enclose the variables, you will raise the error: TypeError: not enough arguments for format string. Let’s look at examples of the error.
Example #1: Incorrect String Formatting Syntax
In the following example, we have a list of ice cream flavors. We want to format a string called popular_flavors to include the four flavors and print the resultant string to the console.
ice_cream_flavors = ["Vanilla", "Strawberry", "Chocolate", "Pistachio"]
popular_flavors = "%s, %s, %s, and %s are popular flavors of ice cream." % ice_cream_flavors[0], ice_cream_flavors[1], ice_cream_flavors[2], ice_cream_flavors[3]
print(popular_flavors)
Let’s run the code to get the result.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
1 popular_flavors = "%s, %s, %s, and %s are popular flavors of ice cream." % ice_cream_flavors[0], ice_cream_flavors[1], ice_cream_flavors[2], ice_cream_flavors[3]
TypeError: not enough arguments for format string
The program throws the error because we did not enclose the arguments in parentheses.
Solution #1: Use Parentheses
To solve the problem, we need to wrap the arguments in parentheses (), as shown below:
popular_flavors = "%s, %s, %s, and %s are popular flavors of ice cream." % (ice_cream_flavors[0], ice_cream_flavors[1], ice_cream_flavors[2], ice_cream_flavors[3])
print(popular_flavors)
Let’s run the code to get the result:
Vanilla, Strawberry, Chocolate, and Pistachio are popular flavors of ice cream.
Solution #2: Use format()
Alternatively, we can use the format() method. The .format() syntax differs from the % string formatting syntax. We need to use curly brackets {} as placeholders for our arguments in the format string and then call the format() method on that string with the required arguments. Let’s look at the revised code:
popular_flavors = "{}, {}, {}, and {} are popular flavors of ice cream.".format(ice_cream_flavors[0], ice_cream_flavors[1], ice_cream_flavors[2], ice_cream_flavors[3])
print(popular_flavors)
Let’s run the code to get the result:
Vanilla, Strawberry, Chocolate, and Pistachio are popular flavors of ice cream.
Solution #3: 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:
print(f"{ice_cream_flavors[0]}, {ice_cream_flavors[1]}, {ice_cream_flavors[2]}, and {ice_cream_flavors[3]} are popular flavors of ice cream.")
Vanilla, Strawberry, Chocolate, and Pistachio are popular flavors of ice cream.
The f-string provides a concise and convenient way to embed python expressions inside a string literal for formatting.
Example #2: Not Enough Arguments
Another common source of the error is not having the correct number of arguments to format the string. Let’s look at our ice cream flavors example:
ice_cream_flavors = ["Vanilla", "Strawberry", "Chocolate", "Pistachio"]
popular_flavors = "%s, %s, %s, and %s are popular flavors of ice cream." % (ice_cream_flavors[0], ice_cream_flavors[1], ice_cream_flavors[2])
print(popular_flavors)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
1 popular_flavors = "%s, %s, %s, and %s are popular flavors of ice cream." % (ice_cream_flavors[0], ice_cream_flavors[1], ice_cream_flavors[2])
TypeError: not enough arguments for format string
The program throws the error because the format string has four argument specifiers, but we only pass three variables.
Solution: Use Correct Number of Arguments
We can solve this error by ensuring all variables present to format the string. Let’s look at the revised code:
popular_flavors = "%s, %s, %s, and %s are popular flavors of ice cream." % (ice_cream_flavors[0], ice_cream_flavors[1], ice_cream_flavors[2], ice_cream_flavors[3])
print(popular_flavors)
All four variables are present. Let’s run the code to get the result:
Vanilla, Strawberry, Chocolate, and Pistachio are popular flavors of ice cream.
This error can also happen if we have more variables than argument specifiers in our format string.
Summary
Congratulations on reading to the end of this tutorial! The error “TypeError: not enough arguments for format string” occurs when the number of arguments specified in the string formatting operation is not equal to the number of variables you want to add into the string.
First, ensure the number of variables equals the number of argument specifiers to solve this error. Then ensure that all variables are in tuple format with parenthesis and comma-separated if you use the % operator. You can use the format() method instead of the % operator. If you are using Python 3.6+, you can use the f-String approach, which has the simplest syntax and is the fastest type of string formatting.
For further reading on string manipulation, go to the article: How to Convert a String to an Int in Python.
For further reading on TypeErrors involving string formatting, go to the article: How to Solve Python TypeError: not all arguments converted during string formatting.
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.