taylor briee
taylor briee

Reputation: 41

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)

Upvotes: 0

Views: 39

Answers (2)

Tobias
Tobias

Reputation: 947

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.

Upvotes: 0

meW
meW

Reputation: 3967

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)

Upvotes: 1

Related Questions