Rebeka
Rebeka

Reputation: 1

typeerror: not enough arguments for format string PYTHON

I'm learning python 2.7.8.I don't know whats wrong,I've tried all the answers posted. Here's the code:

formatterb = "%r %r %r %r"
print formatterb % (
    "I am tired", 
    "Not so tired tho"
    "this had better work.", 
    "been at this for a while")

And here' the error message I get:

TypeError: not enough arguments for format strings.

Upvotes: 0

Views: 735

Answers (1)

khampson
khampson

Reputation: 15306

You are missing a comma between "Not so tired tho" "this had better work.", so there are therefore not enough arguments.

It should be:

formatterb = "%r %r %r %r"
print formatterb % (
    "I am tired", 
    "Not so tired tho",
    "this had better work.", 
    "been at this for a while")

Edit in response to comment from OP:

You can't have 4 format specifiers in the string but only 3 elements in the accompanying tuple. If you want the 2nd and 3rd arguments to be on the same line, they should be the same argument, i.e. "Not so tired tho this had better work".

If you want items on separate lines, in general you can add a newline, e.g.:

formatterb = "%r %r %r"
print formatterb % (
    "I am tired", 
    "Not so tired tho\nthis had better work.", 
    "been at this for a while")

The \n will add a newline in between the two substrings. In either case you would want to remove one of the format specifiers.

However, also note that %r is equivalent to repr, so you'll see the actual string contents, not as it would be printed. e.g. even with the newline, you will see \n and not an actual newline.

If you replace %r with %s, it will show how it would be printed, and you would see it output like this:

I am tired Not so tired tho
this had better work. been at this for a while

Upvotes: 6

Related Questions