Python strings must be formatted using the correct number of arguments. When the number of arguments you specify is less than the number of values you want to format into a string, you encounter an error like “TypeError: not enough arguments for format string”.
In this guide, we talk about what this error means and why it is raised. We walk through an example to help you understand how you can solve this error in your code.
TypeError: not enough arguments for format string
Our error is a TypeError. This means we have tried to do something on a value whose underlying data type does not support that operation.
In this case, we work with string formatting. Python tells us that we have made a mistake formatting a string.
When you format a string, the number of arguments you specify must be equal to the number of values you want to format. Otherwise, you encounter an error.
An Example Scenario
A common situation where this error is raised is when you forget to enclose the arguments in a % string format in parenthesis. This syntax uses the % operator to add values into a string.
Here, we build an app that formats a list of student names who are on the honor roll into a string. We start by defining a list of student names:
students = ["Andy", "Malcolm", "Lindsay", "Lucy"]
Next, we format these values into a string using the % syntax:
honor_roll = "%s, %s, %s, and %s are on the CK High School Honor Roll." % students[0], students[1], students[2], students[3] print(honor_roll)
We format all the four names from our “students” array into the “honor_roll” string. We use %s inside our string to indicate where the values should be formatted. The % is used after the string to specify which values we want to add into our string.
Run our code and see what happens:
Traceback (most recent call last): File "main.py", line 3, in <module> honor_roll = "%s, %s, %s, and %s are on the CK High School Honor Roll." % students[0], students[1], students[2], students[3] TypeError: not enough arguments for format string
Python has encountered an error. Let’s see how we can solve it.
Solution #1: Fix String Formatting Syntax
We’ve made a mistake in how we format strings. When using the % operator, the values you want to add into a string must be enclosed within parentheses.
In our above example, we specify the values to add inside our string as a list of comma-separated values. This list does not appear in parentheses.
We fix our error by enclosing the values following the % sign in parenthesis:
honor_roll = "%s, %s, %s, and %s are on the CK High School Honor Roll." % (students[0], students[1], students[2], students[3])
Let’s run our code:
Andy, Malcolm, Lindsay, and Lucy are on the CK High School Honor Roll.
Our code successfully returns a message informing us of the students on the honor roll.
Solution #2: Use .format()
The % syntax is starting to become outdated. There are other methods of formatting strings such as the .format() method which are more favored in modern Python code.
To solve our problem, we could use the .format()
syntax instead of the % syntax:
honor_roll = "{}, {}, {}, and {} are on the CK High School Honor Roll.".format(students[0], students[1], students[2], students[3])
The structure of this syntax is similar to that of the % method. In our string, we use {} brackets to indicate where we want to add values.
We use the .format()
method to specify the list of values that we want to add into our string. Let’s run our code:
Andy, Malcolm, Lindsay, and Lucy are on the CK High School Honor Roll.
Our program successfully shows us a list of the names of students who are on the honor roll. This list is formatted as part of a string.
Conclusion
The “TypeError: not enough arguments for format string” error is raised when the number of arguments specified in a string format operation is not equal to the number of values you want to add into a string.
To solve this problem, first ensure you enclose all arguments in curly brackets () if you are using the % operator. You can use the .format()
method as a replacement for the % syntax if you want to fix this error and modernize your code.
Now you’re ready to fix this common Python error like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.