TypeError: not enough arguments for format string when using %s

30,479

You need a set of parenthesis:

>>> '%s %s' % 'a', 'b'  # what you have
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>>
>>>
>>> '%s %s' % ('a', 'b')  # correct solution
'a b'

'%s %s' % 'a', 'b' is evaluated as ('%s %s' % 'a'), 'b', which produces an error in '%s %s' % 'a' since you have fewer arguments than format specifiers.


print("So your name is %s, your last name is %s, you are %s and you are %s years old" % (name, last_name, gender, age))
Share:
30,479
beepbeep
Author by

beepbeep

Updated on July 09, 2022

Comments

  • beepbeep
    beepbeep almost 2 years

    this is my code

    import sys
    name = input("Enter your name:")
    last_name = input("Enter your last name:")
    gender = input("Enter your gender:")
    age = input("Enter your age:")
    print ("So your name is %s, your last name is %s, you are %s and you are %s years old" % name, last_name, gender, age)
    

    I've searched the topic but I don't understand.