读书人

python打印疏失

发布时间: 2013-09-28 10:01:20 作者: rapoo

python打印出错
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print "hello world"
SyntaxError: invalid syntax

为啥打印不出来呢
[解决办法]
改变结尾方式为
print(内容, end='行尾内容')
end省略,默认就是换行
[解决办法]
有说明,3.x的新特性
参考:http://docs.python.org/3/whatsnew/3.0.html

Print Is A Function
The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105). Examples:

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline

Old: print # Prints a newline
New: print() # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y) # prints repr((x, y))
New: print((x, y)) # Not the same as print(x, y)!
You can also customize the separator between items, e.g.:

print("There are <", 2**32, "> possibilities!", sep="")
which produces:

There are <4294967296> possibilities!
Note:

The print() function doesn’t support the “softspace” feature of the old print statement. For example, in Python 2.x, print "A\n", "B" would write "A\nB\n"; but in Python 3.0, print("A\n", "B") writes "A\n B\n".
Initially, you’ll be finding yourself typing the old print x a lot in interactive mode. Time to retrain your fingers to type print(x) instead!
When using the 2to3 source-to-source conversion tool, all print statements are automatically converted to print() function calls, so this is mostly a non-issue for larger projects.

读书人网 >perl python

热点推荐