TypeError: not enough arguments for format string

In python, the error “TypeError: not enough arguments for format string” occurs because the number of format specifiers is more than the number of values passed. Also, this error occurs when you fail to add a parenthesis to the values passed.

The string format specifyer is used to dynamically replace the value in the string. The string uses percent (“%”) to specify the formats. The new string format specifyer uses the curly braces. The number of format specified and the number of values passed should be same.

All values passed in the string should be enclosed in the parenthesis. Otherwise, the python interpreter will only treat the first value, and the rest of the values will be ignored. In this case, the format specifier number is more than the values passed, so the python interpreter will throw “TypeError: not enough arguments for the format string” error.



Exception

The type error stack trace will be shown as below

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 4, in <module>
    print "The values are %s %s" % x, y
TypeError: not enough arguments for format string
[Finished in 0.1s with exit code 1]


Root Cause

This type error is due to the following reasons.

  • The number of format specifier is more than the number of values passed
  • The values are not enclosed by a parenthesis.


Solution 1

All values passed in a string should be enclosed with a parenthesis. Otherwise, the python interpreter will only treat the first value and the rest of the values will be ignored. In this case, the number of format specifier is more than the values passed, so the python interpreter throws “TypeError: not enough arguments for the format string” error.

print "The values are %s %s" % x, y

will be treated as below and y will be ignored

print ("The values are %s %s" % x), y

Program

x = 5
y = 10
z = 15
print "The values are %s %s" % x, y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 4, in <module>
    print "The values are %s %s" % x, y
TypeError: not enough arguments for format string
[Finished in 0.1s with exit code 1]

Solution

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

Output

The values are 5 10
[Finished in 0.0s]


Solution 2

In python, if the number of format specifier is more than the value passed, the python interpreter can not replace all the formats in the string. The value of the missing format specifier must be added in the string. This is going to solve the type error.

Program

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

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 4, in <module>
    print "The values are %s %s %s" % (x, y)
TypeError: not enough arguments for format string
[Finished in 0.0s with exit code 1]

Solution

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

Output

The values are 5 10 15
[Finished in 0.1s]


Solution 3

In python, if the number of format specifier is more than the value passed, the python interpreter can not replace all the formats in the string. The value of the missing format specifier must be added in the string. This is going to solve the type error.

Program

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

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 4, in <module>
    print "The values are %s %s %s" % (x, y)
TypeError: not enough arguments for format string
[Finished in 0.0s with exit code 1]

Solution

x = 5
y = 10
z = 15
print "The values are {0} {1} {2}".format(x, y, z)

Output

The values are 5 10 15
[Finished in 0.1s]


Solution 4

In python, if the percent symbol requires printing in the print statement, it should be added twice, otherwise this type error will be thrown. The python interpreter expects the format character after the percent symbol in the string. To print a percent, the percent symbol must be added twice in the string. This is going to solve the type error.

Program

x = 5
y = 10
z = 15
print "The percentage is %s%s" % x

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 4, in <module>
    print "The percentage is %s%" % x
ValueError: incomplete format
[Finished in 0.0s with exit code 1]

Solution

x = 5
y = 10
z = 15
print "The percentage is %s%%s" % x

Output

The percentage is 5%s
[Finished in 0.0s]



Related Articles

Leave a Reply

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