繁体   English   中英

将变量和换行符放入Python的打印语句中

[英]Putting variables and line breaks into print statements in Python

我在此代码中不断收到语法错误。 有人可以在这里看到任何格式错误吗?

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)

这是更正的一个:

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)

应该是这样的:

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

\\n应该是一个字符串。 反斜杠(\\)字符用于转义具有特殊含义的字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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