Python TypeError: not enough arguments for format string

Question:

Here’s the output. These are utf-8 strings I believe… some of these can be NoneType but it fails immediately, before ones like that…

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

TypeError: not enough arguments for format string

Its 7 for 7 though?

Asked By: y2k

||

Answers:

You need to put the format arguments into a tuple (add parentheses):

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

What you currently have is equivalent to the following:

intstr = ("'%s', '%s', '%d', '%s', '%s', '%s', '%s'" % softname), procversion, int(percent), exe, description, company, procurl

Example:

>>> "%s %s" % 'hello', 'world'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> "%s %s" % ('hello', 'world')
'hello world'
Answered By: Andrew Clark

Note that the % syntax for formatting strings is becoming outdated. If your version of Python supports it, you should write:

instr = "'{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}'".format(softname, procversion, int(percent), exe, description, company, procurl)

This also fixes the error that you happened to have.

Answered By: Simeon Visser

I got the same error when using % as a percent character in my format string. The solution to this is to double up the %%.

Answered By: Bruce Jakeway

I had the same issue I was using a raw query for a specific reason and this was to add double quotes in TIME_FORMAT function.

User.objects.raw(
            f'SELECT f1,f2,TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(end_time) - TIME_TO_SEC(start_time))),"%%H:%%i") AS log FROM users GROUP BY start_dt')
Answered By: Hardik Raval

Please solve this code also getting same error
———————————————-

import mysql.connector as con
db=con.connect(host="localhost",user="root",passwd="12345678")
cursor=db.cursor()
dbname=("nakul")
udb="use %s"%dbname
cursor.execute(udb)
ch="y"
c=[]
while ch=="Y" or ch=="y":
    print("1.Create Tablen","2.Add Columnsn","3.Insert Value In Tablen","4.Update Datan","5.Delete Datan","6.Delete Table")
    c=int(input("Choose An Option:"))
    if c==1:
        tbn=input("Enter table name:")
        tval=input("Enter table name with data type and key:")
        ctb=("create table %s(%s)")%tbn%tval
        cursor.execute(ctb)
        db.commit 
Answered By: NAKULOP1
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.