简体   繁体   中英

Very basic string formatting not working (Python)

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).

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, eg

 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, eg

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.

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))

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM