Python 中是否可以将长行分成多行?
就像C 一样,你可以将一条长线分成多条短线。但是在 Python 中,如果我这样做,将会出现缩进错误。 。 是否可以?
Just like C, you can break a long line into multiple short lines. But in Python, if I do this, there will be an indent error... Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自 PEP 8 - Python 代码风格指南:
隐式续行示例:
关于二元运算符的换行主题,它继续说道:
显式续行示例:
From PEP 8 - Style Guide for Python Code:
Example of implicit line continuation:
On the topic of line breaks around a binary operator, it goes on to say:
Example of explicit line continuation:
有不止一种方法可以做到这一点。
1).长篇大论:
2)。使用括号:
3)。再次使用
\
:引用 PEP8:
There is more than one way to do it.
1). A long statement:
2). Using parenthesis:
3). Using
\
again:Quoting PEP8:
如果你想给变量分配一个长字符串,你可以这样做:
不要添加任何逗号,否则你会得到一个包含很多字符串的元组!
If you want to assign a long string to variable, you can do it as below:
Do not add any comma, or you will get a tuple which contains many strings!
它也适用于 Python:
It works in Python too:
当尝试输入连续文本(例如查询)时,不要在行尾放置逗号,否则您将得到一个字符串列表而不是一个长字符串:
有点像那样。
acgtyrant
有这样的评论,抱歉,没看到。 :/When trying to enter continuous text (say, a query) do not put commas at the end of the line or you will get a list of strings instead of one long string:
kinda like that.
There is a comment like this from
acgtyrant
, sorry, didn't see that. :/与数据库相关的代码在多行中看起来更容易,并用一对三引号括起来:
比下面的一行巨长的代码更容易看:
DB related code looks easier on the eyes in multiple lines, enclosed by a pair of triple quotes:
than the following one giant long line:
据我所知,这是可以做到的。 Python 对于三引号字符串(
"""like this"""
)具有隐式续行(在括号、方括号和字符串内),并且续行的缩进并不重要。有关更多信息,您可能需要阅读关于 Python 词法分析的这篇文章。组织。As far as I know, it can be done. Python has implicit line continuation (inside parentheses, brackets, and strings) for triple-quoted strings (
"""like this"""
) and the indentation of continuation lines is not important. For more information, you may want to read this article on lexical analysis, from python.org.