S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

print SyntaxError: invalid syntax错误的问题

Posted on 2010-06-07 14:16 S.l.e!ep.¢% 阅读(2415) 评论(0)  编辑 收藏 引用 所属分类: python
print SyntaxError: invalid syntax错误的问题
2010-05-10 15:19
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:

01Old: print"The answer is", 2*2
02New: print("The answer is", 2*2)
03
04Old: printx,           # Trailing comma suppresses newline
05New: print(x, end=" ") # Appends a space instead of a newline
06
07Old: print              # Prints a newline
08New: print()            # You must call the function!
09
10Old: print>>sys.stderr, "fatal error"
11New: print("fatal error", file=sys.stderr)
12
13Old: print(x, y)       # prints repr((x, y))
14New: print((x, y))      # Not the same as print(x, y)!


You can also customize the separator between items, e.g.:
1print("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.

操作如下:

										
>>> print " Hello World! "
File
" <stdin> " , line 1
print " Hello World! "
^
SyntaxError: invalid syntax
>>>

出错原因:

    python v3.0以后的版本中将v2.x版本的print 改为了print().

   所以此处调用print("Hello World!")则可成功。


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理