语法怪癖或者为什么是有效的python
In python 2.6, why is the following line valid?
在python 2.6中,为什么以下行是有效的?
my_line = 'foo' 'bar'
and if that is valid, why isn't the following:
如果这是有效的,为什么不是下面的
my_list = 1 2
The first example is string concatenation, however, the following isn't valid either (thanks god):
第一个例子是字符串连接,但是,以下也不是有效的(感谢上帝):
foo = 'foo'
bar = 'bar'
foo_bar = foo bar
4 个解决方案
#1
20
This is doing string literal concatenation. As noted in the documentation, advantages include the following:
这是字符串文字连接。如文件所指出,优点包括:
This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings...
这个特性可以用来减少需要的反斜杠的数量,可以方便地跨长行分割长字符串,甚至可以向字符串的部分添加注释……
It goes on to note that this concatenation is done at compilation time rather than run time.
它接着指出,这种连接是在编译时完成的,而不是在运行时完成的。
The history and rationale behind this, and a rejected suggestion to remove the feature, is described in PEP 3126.
在PEP 3126中描述了这一特性背后的历史和原理,以及一个被拒绝的删除该特性的建议。
更多相关文章
- Python - 去除字符串首尾填充
- Python处理字符串
- Python语言特性之1:函数参数传递
- python list range 字符串的截取 如 text[1:5]
- 在Python中使用正则表达式匹配的字符串周围添加括号
- 用python将二进制整数或字符串写入文件
- python1.返回一个字符串中出现次数第二多的单词 2.字符串中可能
- Python笔记(九):字符串操作
- python 产生随机数,随机字符串