在python中,开放paren会导致后续行被认为是同一行的一部分,直到关闭为止。
所以,你可以这样做:
def game(word, con):
return (word + str('!') +
word + str(',') +
word + str(phrase1))
但我不会建议,在这种特殊情况下。我提到它,因为它在语法上是有效的,你可以在别处使用它。
你可以做的另一件事是使用反斜杠:
def game(word, con):
return word + '!' + \
word + ',' + \
word + str(phrase)
# Removed the redundant str('!'), since '!' is a string literal we don't need to convert it
或者说,在这种特殊情况下,我的建议是使用的格式化字符串。
def game(word, con):
return "{word}!{word},{word}{phrase1}".format(
word=word, phrase1=phrase1")
看起来它在功能上等同于你在你身上所做的事情,但我无法真正了解。后者是我在这种情况下做的。
如果你想在STRING中换行,那么你可以在任何你需要的地方使用“\ n”作为字符串。
def break_line():
return "line\nbreak"
“为了让测试返回'真正',我需要在我的返回语句中有换行符。” - 不,你不知道。最有可能的是,你想在你要返回的字符串中换行,而不是在return语句本身。这可以用'\ n'来完成。 – user2357112
“interwebs”? – glglgl