简体   繁体   中英

Putting variables and line breaks into print statements in Python

I keep getting a syntax error in this code. Can anybody see any formatting errors here?

for i in range (10,-1,-1):
    b="green bottles sitting on the wall"
    print(i,b,\n,i,b,\n,"And if one green bottle should accidentally fall"\n"There will be",i,b)

Here's the corrected one:

for i in range (10,-1,-1):
    b = "green bottles sitting on the wall"
    print(i, b, '\n', i, b, '\nAnd if one green bottle should accidentally fall\nThere will be', i, b)

It should be like this:

print(i,b,"\n",i,b,"\n","And if one green bottle should accidentally fall\nThere willbe",i,b)

\\n should be a string. The backslash (\\) character is used to escape characters that otherwise have a special meaning.

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