我有一长串代码,我希望在多行之间分解.我使用什么,语法是什么?
例如,添加一串字符串,
e = 'a' + 'b' + 'c' + 'd'
Run Code Online (Sandbox Code Playgroud)
并将它分成两行:
e = 'a' + 'b' +
'c' + 'd'
Run Code Online (Sandbox Code Playgroud) 由于PEP8建议保持低于你的python程序的80列规则,我怎么能遵守长字符串,即
s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten."
Run Code Online (Sandbox Code Playgroud)
我将如何将其扩展到以下行,即
s = "this is my really, really, really, really, really, really" +
"really long string that I'd like to shorten."
Run Code Online (Sandbox Code Playgroud) 在尝试遵守python样式规则时,我将编辑器设置为最多79列.
在PEP中,它建议在括号,括号和括号内使用python隐含的延续.但是,当我达到col限制时处理字符串时,它会有点奇怪.
例如,尝试使用多行
mystr = """Why, hello there
wonderful stackoverflow people!"""
Run Code Online (Sandbox Code Playgroud)
将返回
"Why, hello there\nwonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
这有效:
mystr = "Why, hello there \
wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
因为它返回:
"Why, hello there wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
但是,当语句缩进几个块时,这看起来很奇怪:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there \
wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
如果您尝试缩进第二行:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there \
wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
你的字符串最终为:
"Why, hello there wonderful stackoverflow …Run Code Online (Sandbox Code Playgroud) 如果我有一个很长的代码行,是否可以在下一行继续它,例如:
url='http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
+ '100,000|1,000,000&chxp=1,0&chxr=0,0,' +
max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:'
Run Code Online (Sandbox Code Playgroud)