Not Enough Arguments For Format String Python Error
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers not enough arguments for format string django or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x typeerror not enough arguments for format string mysql Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it not enough arguments for format string python sql only takes a minute: Sign up TypeError: not enough arguments for format string when using %s up vote 6 down vote favorite this is my code import sys name = input("Enter your name:") last_name = input("Enter your last name:") gender not enough arguments for format string atmel studio = 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. python string string-formatting typeerror share|improve this question edited Jun 16 '14 at 21:32 iCodez 77.2k16108136 asked Jun 16 '14 at 21:16 user3746426 29113 add a comment| 2 Answers 2 active oldest votes up vote 11 down vote accepted
Not Enough Arguments Passed For Format String
You need to put your arguments for string formatting in parenthesis: print (... % (name, last_name, gender, age)) Otherwise, Python will only see name as an argument for string formatting and the rest as arguments for the print function. Note however that using % for string formatting operations is frowned upon these days. The modern approach is to use str.format: print ("So your name is {}, your last name is {}, you are {} and you are {} years old".format(name, last_name, gender, age)) share|improve this answer answered Jun 16 '14 at 21:19 iCodez 77.2k16108136 add a comment| up vote 6 down vote You need a set of parenthesis: >>> '%s %s' % 'a', 'b' # what you have Traceback (most recent call last): File "", line 1, in 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|improve this answer answered Jun 16 '14 at 21:20 arshajii 80.2k15136207 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook S
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
Python Escape Percent
Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is sqlalchemy typeerror not enough arguments for format string a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up TypeError: not enough arguments for format indexerror: tuple index out of range string - using a While Loop up vote 2 down vote favorite I'm doing the old 99 bottles song and trying to do it using a While loop, to help me continue to better learn that loop type. I'm wondering why I would http://stackoverflow.com/questions/24252358/typeerror-not-enough-arguments-for-format-string-when-using-s be getting a TypeError when in my code below and what arguments exactly I'm missing? Here is my code: # Get number of beers bottles = int(raw_input("How many bottles of beer? ")) # return invalid response if bottles < 1: print "That's not a good number" if bottles == 1: s1 = "bottle" s2 = "bottles" elif bottles == 2: s1 = "bottles" s2 = "bottles" # sing verses while bottles > 0: print "%d %s of beer on the wall," % bottles, s1 print "%d %s of http://stackoverflow.com/questions/13896612/typeerror-not-enough-arguments-for-format-string-using-a-while-loop beer." % bottles, s1 print "You take one down, pass it around," print "%d %s of beer on the wall." % (bottles - 1), s2 print bottles -= 1 And here is the error: Traceback (most recent call last): File "beer.py", line 47, in print "%d %s of beer on the wall," % bottles, s1 TypeError: not enough arguments for format string I've tried using parenthesis around the "bottles, s1" after the %, but still doesn't help. Any suggestions? python share|improve this question asked Dec 15 '12 at 22:00 Jason Rueckert 154129 Using (bottles, s1) works for me. –Tim Dec 15 '12 at 22:02 What if the input for bottles is 10. Where does s1 and s2 get defined? –jdi Dec 15 '12 at 22:03 Your code will generate a NameError if you set bottles to anything larger than 2, since in that case neither of your if/elif will execute. You should change the elif to an if. Also it looks like you have an indentation problem. The if/elif should not be under the if bottles < 1. –BrenBarn Dec 15 '12 at 22:04 And it also would never update your s1 and s2 strings since they only get evaluated once from the user input. The while loop will just spin until it'd done. You would need to move them inside of the while loop so they get set properly each time. –jdi Dec 15 '12 at 22:05 I guess I'm still just confused as to how I should be defin
not enough arguments for format string Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] ----- Message from john at fouhy.net --------- Date: Wed, 2 Jul 2008 https://mail.python.org/pipermail/tutor/2008-July/062793.html 16:49:19 +1200 From: John Fouhy > On 02/07/2008, Christopher Spears http://www.gossamer-threads.com/lists/python/dev/727965 wrote: >> File "point.py", line 13, in __str__ >> point_str = "(%f,%f)" % self.x, self.y >> TypeError: not enough arguments for format string >> >> Does anyone know what is wrong? I'm sure it is something obvious, >> but I can't see it. > > Hi Christopher, > > Here's not enough the short answer: You need to put brackets around "self.x, self.y" > i.e. point_str = "(%f,%f)" % (self.x, self.y) > > The long answer: Python interprets the statement "point_str = > "(%f,%f)" % self.x, self.y" as "point_str = ("(%f,%f)" % self.x), > self.y". There are two "%f" expressions in the string, but you only > supplied one argument, self.x. Thus python tells you that there > not enough arguments aren't enough arguments. To deal with this, you need to supply the > arguments as a tuple. Hi all, While it is true that you need to put parenthesis around the arguments, it isn`t quite the case that they are needed so as to provide the arguments as a tuple: >>> a = 42, 42 >>> type(a) >>> It is the comma, not the parens that make for a tuple. My guess is that the parens are needed to disambiguate the end of the arguments as the comma can also indicate that another element is to be printed on the same line: >>> print '%s' %42 , 333 42 333 >>> In a case like >>> print '%s%s' %42 , 333 ... TypeError: not enough arguments for format string it would be ambiguous whether 333 was intended as a second formatting argument or as a second thing to be printed. Best, Brian vdB Previous message: [Tutor] TypeError: not enough arguments for format string Next message: [Tutor] TypeError: not enough arguments for format string Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] More information about the Tutor mailing list
Post #1 of 5 (9866 views) Permalink Problem with string format Hello, I have a strange problem with the string format: >>> '%s %s %s %s %s' % ['01', '02', '03', '04', '05'] Traceback (most recent call last): File "", line 1, in TypeError: not enough arguments for format string But as soon I use tuple it is working: >>> '%s %s %s %s %s' % ('01', '02', '03', '04', '05') '01 02 03 04 05' What is the problem and how can I still use list? Thanks. -- http://mail.python.org/mailman/listinfo/python-list steven at REMOVE Mar10,2009,10:17PM Post #2 of 5 (9779 views) Permalink Re: Problem with string format [In reply to] On Tue, 10 Mar 2009 22:07:59 -0700, Mike314 wrote: > Hello, > > I have a strange problem with the string format: > >>>> '%s %s %s %s %s' % ['01', '02', '03', '04', '05'] > Traceback (most recent call last): > File "", line 1, in > TypeError: not enough arguments for format string > > But as soon I use tuple it is working: >>>> '%s %s %s %s %s' % ('01', '02', '03', '04', '05') > '01 02 03 04 05' > > > What is the problem and how can I still use list? There is no problem and you can't. As far as string formatting is concerned, a list is a single object which goes into a single %s. Try converting the list into a tuple first: '%s %s %s %s %s' % tuple(['01', '02', '03', '04', '05']) -- Steven -- http://mail.python.org/mailman/listinfo/python-list mensanator at aol Mar10,2009,10:18PM Post #3 of 5 (9785 views) Permalink Re: Problem with string format [In reply to] On Mar 11, 12:07 am, Mike314 wrote: > Hello, > > I have a strange problem with the string format: > > >>> '%s %s %s %s %s' % ['01', '02', '03', '04', '05'] > > Traceback (most recent call last): > File "", line 1, in > TypeError: not enough arguments for format string > > But as soon I use tuple it is working:>>> '%s %s %s %s %s' % ('01', '02', '03', '04', '05') > > '01 02 03 04 05' > > What is the problem and how ca