为什么 Python 的打印函数会这样?
Why does Python's print function act this way?
我正在编写一个简单的 Hello World Python 脚本并尝试了一些不同的东西以查看我会得到什么。
结果:
print "Hello World" ===> Hello World
print "Hello"," ","World ===> Hello World
print "Hello" "World" ===> HelloWorld
结果让我很吃惊……根据其他语言的经验,我希望得到更多类似这样的东西:
print "Hello World" ===> Hello World
print "Hello"," ","World ===> Hello World
print "Hello" "World" ===> Syntax Error!
在尝试了更多示例后,我意识到每当您用“,”分隔字符串时,它似乎都会添加一个 space。
...更奇怪的是,它似乎并不关心你是否给它多个字符串而不用“,”分隔它们,如第三个示例所示。
为什么 Python 的打印函数会这样???
还有办法阻止它为“,”分隔的字符串添加 spaces 吗?
因为 print
语句在 分隔值 之间添加了 spaces,如 documented:
A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line.
但是,"Hello" "World"
不是两个值;它是 one 字符串。只有两个字符串文字之间的 whitespace 被忽略,这些字符串文字被连接起来 (by the parser):
>>> "Hello" "World"
"HelloWorld"
参见 String literal concatenation section:
Multiple adjacent string literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation.
这使得组合不同的字符串文字样式变得更加容易(三重引号和原始字符串文字以及 'regular' 字符串文字都可以用来创建一个值),以及创建一个 long 字符串值更容易格式化:
long_string_value = (
"This is the first chuck of a longer string, it fits within the "
'limits of a "style guide" that sets a shorter line limit while '
r'at the same time letting you use \n as literal text instead of '
"escape sequences.\n")
这个特性其实是继承自C,并不是Python发明的
在 Python 3 中,print()
is a function 而不是 语句 ,您可以更好地控制如何处理多个参数。单独的参数由函数的 sep
参数分隔,默认为 space.
在 Python 2 中,您可以通过将 from __future__ import print_function
添加到模块顶部来获得相同的功能。这将禁用该语句,从而可以使用 same function in Python 2 code.
我正在编写一个简单的 Hello World Python 脚本并尝试了一些不同的东西以查看我会得到什么。
结果:
print "Hello World" ===> Hello World
print "Hello"," ","World ===> Hello World
print "Hello" "World" ===> HelloWorld
结果让我很吃惊……根据其他语言的经验,我希望得到更多类似这样的东西:
print "Hello World" ===> Hello World
print "Hello"," ","World ===> Hello World
print "Hello" "World" ===> Syntax Error!
在尝试了更多示例后,我意识到每当您用“,”分隔字符串时,它似乎都会添加一个 space。
...更奇怪的是,它似乎并不关心你是否给它多个字符串而不用“,”分隔它们,如第三个示例所示。
为什么 Python 的打印函数会这样???
还有办法阻止它为“,”分隔的字符串添加 spaces 吗?
因为 print
语句在 分隔值 之间添加了 spaces,如 documented:
A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line.
但是,"Hello" "World"
不是两个值;它是 one 字符串。只有两个字符串文字之间的 whitespace 被忽略,这些字符串文字被连接起来 (by the parser):
>>> "Hello" "World"
"HelloWorld"
参见 String literal concatenation section:
Multiple adjacent string literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation.
这使得组合不同的字符串文字样式变得更加容易(三重引号和原始字符串文字以及 'regular' 字符串文字都可以用来创建一个值),以及创建一个 long 字符串值更容易格式化:
long_string_value = (
"This is the first chuck of a longer string, it fits within the "
'limits of a "style guide" that sets a shorter line limit while '
r'at the same time letting you use \n as literal text instead of '
"escape sequences.\n")
这个特性其实是继承自C,并不是Python发明的
在 Python 3 中,print()
is a function 而不是 语句 ,您可以更好地控制如何处理多个参数。单独的参数由函数的 sep
参数分隔,默认为 space.
在 Python 2 中,您可以通过将 from __future__ import print_function
添加到模块顶部来获得相同的功能。这将禁用该语句,从而可以使用 same function in Python 2 code.