gpt4 book ai didi

python 将语句拆分为多行

转载 作者:行者123 更新时间:2023-11-28 20:24:36 25 4
gpt4 key购买 nike

我有以下代码:

print "my name is [%s], I like [%s] and I ask question on [%s]" % ("xxx", "python", "stackoverflow")

我想将这一长行分成多行:

print
"my name is [%s]"
", I like [%s] "
"and I ask question on [%s]"
% ("xxx", "python", "stackoverflow")

能否提供正确的语法?

最佳答案

通过将所有内容放在括号内来使用隐含的行继续。这是Python's Style Guide (PEP 8)中推荐的方法:

print ("my name is [%s]"
", I like [%s] "
"and I ask question on [%s]"
% ("xxx", "python", "stackoverflow"))

这是可行的,因为 Python 解释器将连接相邻的字符串文字,所以 "foo"'bar' 变成了 'foobar'

关于python 将语句拆分为多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17076710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com