TypeError: Not Enough Arguments For Format String

The TypeError: not enough arguments for format string can occur because of a number of reasons which includes not providing enough string values when printing the strings or calling a function. The error can be solved by first identifying the location and then providing the correct number of arguments.

If you are a Python programmer and have been programming for a long time, you might have come across TypeError: not enough arguments for format string error. In my journey in Python programming, I have faced this error numerous times. In this short article, we will go through TypeError: not enough arguments for a format string error, its causes, and solutions. Moreover, we will try to understand the error so that in the future we can easily solve such errors.

TypeError: not enough arguments for format string – Possible Solutions

First, let us try to understand the error. The error occurs when you try to format a string using the format() method but didn’t provide enough arguments. Or it can also occur when we do not provide enough string values.

Let us take a simple example and see how we are getting this error:

# printing the values
x = 5
y = 10
z = 15
print("The values are %s %s %s" % (x, y))

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 5
      3 y = 10
      4 z = 15
----> 5 print("The values are %s %s %s" % (x, y))

TypeError: not enough arguments for format string

As you can see, we got the error. This is not just the only reason or scenario for getting the error. There are many other possible reasons for getting the same error. In the following sections, we will go through some of these cases and will solve them.

Solution-1: Fixing String formatting syntax

In Python, we can add a string to the print statement by specifying the location for the given strings. For example, %s inside the print statements means, we want to add a string in this place which will be provided as an argument to the print statement.

TypeError: not enough arguments for format string

As shown in the picture, if there are two places for the strings, then we must provide two arguments. If we will not provide arguments equal to the specified places, then we will get TypeError: not enough arguments for format string error.

For example, in the following code, we specified two places but provided only one argument.

# printing the values
print("This is %s %s" %("Bashir"))

Output:

TypeError: not enough arguments for format string - Possible Solutions

The reason for getting the error is that we provide only one argument when there was a place for two arguments. It can be easily solved by providing the same number of arguments:

# printing the values
print("This is %s %s" %("Bashir", "alam"))

Output:

This is Bashir alam

As you can see, we solved the error

Solution-2: Using the format() method

Another method to add strings to the print statement is using the format() method. When using the format method, we don’t specify the location for the string using the % operator. We used {} for the specification of the location.

TypeError: not enough arguments for format string

Now let us take an example and see how we can use the format() method in order to get rid of the error:

# printing the values
print("This is {} {}" .format("Bashir", "alam"))

Output:

This is Bashir alam

As you can see, there were two places for the string values and we provided two arguments in the format method.

Solution-3: Using fstring

One of the best ways to solve TypeError: not enough arguments for format string error is to use the fstring. In fstring, we only specify the number of string values along with their values/variables so we do not need to provide the strings as an argument.

For example, see the following example where we are just printing the first name:

first= "Bashir"
last = 'Alam'

s = f'This is {first}'
print(s)

Output:

This is Bashir

If we want to add the second name as well, we need to specify the location along with the values as well.

first= "Bashir"
last = 'Alam'

s = f'This is {first}'
print(s)

Output:

This is Bashir Alam

As you can see, using the fstring method, there are very less chances of getting errors because even if you provide fewer string values, it will execute on them.

Summary

The reason for getting TypeError: not enough arguments for format string is that when we try to print string values and provide specific locations for them but forget to provide the exact number of strings. In this short article, we discussed the various reasons and cases for getting TypeError: not enough arguments for format string error and solved the error using many methods.

Related Issues

3 thoughts on “TypeError: Not Enough Arguments For Format String”

  1. Pingback: ModuleNotFounderror: No Module Named Pycocotools - Techfor-Today

  2. Pingback: SyntaxError: EOL while scanning string literal Solved - Techfor-Today

  3. Pingback: ValueError: Need More than 1 Value to Unpack - Techfor-Today

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top