TypeError: not enough arguments for format string in Python

Dung Do Tien Sep 05 2021 178

Hello you guys, I am a newbie in Python and I'm also studying more about Python.

I have a small assignment, I want to concat some information into a full string. Like this:

softname = "Python"
procversion = "3.8.2"
percent ="21"
exe = "extention"

instr = ("'%s', '%s', '%d', '%s'" % softname, procversion, int(percent), exe)

print(instr)

But I get an exception TypeError: not enough arguments for format string when I run code above.

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    instr = ("'%s', '%s', '%d', '%s'" % softname, procversion, int(percent), exe)
TypeError: not enough arguments for format string

And I am using python 3.8.2

Anyone can explain it to me? How can I solve it?

Thanks for any response.

Have 1 answer(s) found.
  • A

    Akhmir Rudmakh Sep 05 2021

    The syntax is incorrect. String formatting arguments must be a tuple. You create a tuple with a formatted string and a second formatting argument. Use this instead:

    softname = "Python"
    procversion = "3.8.2"
    percent ="21"
    exe = "extention"
    
    instr = ("'%s', '%s', '%d', '%s'" % (softname, procversion, int(percent), exe))
    
    print(instr)

    #Output

    'Python', '3.8.2', '21', 'extention'

    Hope it is helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close