$ cat bla.py
u = unicode('d…')
s = u.encode('utf-8')
print s
$ python bla.py
File "bla.py", line 1
SyntaxError: Non-ASCII character '\xe2' in file bla.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Run Code Online (Sandbox Code Playgroud)
如何在源代码中声明utf-8字符串?
在Python 2.7中,以下两者都是相同的
print("Hello, World!") # Prints "Hello, World!"
print "Hello, World!" # Prints "Hello, World!"
Run Code Online (Sandbox Code Playgroud)
但是以下不会
print("Hello,", "World!") # Prints the tuple: ("Hello,", "World!")
print "Hello,", "World!" # Prints the words "Hello, World!"
Run Code Online (Sandbox Code Playgroud)
在Python 3.x括号中print是必需的,基本上使它成为一个函数,但在2.7中,两者都将使用不同的结果.我还应该print在Python 2.7中了解什么?