在 python 中,左括号会导致后续行被视为同一行的一部分,直到右括号为止。
所以你可以这样做:
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")
这看起来在功能上等同于你在你所做的事情,但我真的不知道。不过,在这种情况下我会做后者。
如果您想在字符串中换行,则可以在任何需要的地方使用“\n”作为字符串文字。
def break_line():
return "line\nbreak"