0

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.

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

4 Comments

What is the name of modulo when used like this?
Yeah,that's because I want them on separate lines not on 1 line @khampson
@kpie - not sure what you're asking. Are you referring to the %r?
Sure, no problem. Please accept the answer since it appears it has answered your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.