4

I am just learning python and attempting a very simple string formatting line, but it's not working properly. As you can see from the output below, it is inserting the number in the last of the 3, but the first 2 are just showing the code instead of the number. I am sure the solution is simple, but I have tried to look at my code vs the code in the learning material I have, and I can't see any difference. Using Python 3.4 in Visual Studio 2015.

Thank you in advance for any available help! :)

CODE (Area is 200)

print("The area of the square would be {0:f} " .format(area))

#This is an example with multiple numbers and use of "\" outside of a string to allow for
#multiple lines of code in the same line spread out over numerous lines
print("Here are three other numbers." + \
    " First number is {0:d}, second number is {1:d}, \n" + \
    "third number is {2:d}" .format(7,8,9))

OUTPUT

The area of the square would be 200.000000

Here are three other numbers. First number is {0:d}, second number is {1:d}, third number is 9.

The thread 'MainThread' (0xc10) has exited with code 0 (0x0).

The program '[4968] python.exe' has exited with code -1073741510 (0xc000013a).

3 Answers 3

4

The problem here is that the format method is only being called for the final string in the 3 strings being added together. You should either apply the format operation AFTER you have concatenated the strings, or use a single string format that accepts line breaks (so you don't have to concatenate strings in the first place).

To apply after concatenation, you can just use an extra set of parentheses around the string concatenation, e.g.

 print(("Here are three other numbers." + \
" First number is {0:d}, second number is {1:d}, \n" + \
"third number is {2:d}") .format(7,8,9))

Or you can use triple quotes to accept line breaks, e.g.

print("""Here are three other numbers.\
First number is {0:d}, second number is {1:d},
third number is {2:d}""" .format(7,8,9))

where \ allows a line break in the code that wont be printed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. Your response is very clear and easy to understand.
2

You have a precedence issue. What you are actually doing is concatinating three strings - "Here are three other numbers.", " First number is {0:d}, second number is {1:d}, \n" and the result of "third number is {2:d}" .format(7,8,9). The format call is only applied to the third string, and therefore only replaces the {2:d}.

One way to solve this could be to surround the string concatination you intended to have with parentheses (()) so it's evaluated first:

print(("Here are three other numbers." + \
    " First number is {0:d}, second number is {1:d}, \n" + \
    "third number is {2:d}") .format(7,8,9))

But a much cleaner approach would be to drop the string concatination altogether and just use a multiline string (note the lack of the + operator):

print("Here are three other numbers." \
    " First number is {0:d}, second number is {1:d}, \n" \
    "third number is {2:d}" .format(7,8,9))

1 Comment

Wow, thank you all for your quick and easy to understand responses. I thought that the format would apply to all {} fields within the same parenthesis, however, the responses make sense now that I re-look at what I have written as the concatenation has separated the strings within the parenthesis, where I thought all strings within a single parenthesis were included together.
0

Your string is splitted into two (actually three) parts, but the first string is not really relevant, so we leave it out. However, the formatting is only applied to the last string, in this case "third number is {2:d}", so the format strings in the string "First number is {0:d}, second number is {1:d}, \n" are simply ignored.

What worked for me was the following code:

print("Here are three other numbers." + " First number is {0:d}, second number is {1:d}, \n third number is {2:d}".format(7,8,9))

Comments

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.