码迷,mamicode.com
首页 > 编程语言 > 详细

Python2和Python3中print的不同点

时间:2017-11-09 11:48:32      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:参数   hello   close   std   sed   hid   必须   换行   splay   

在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异

主要体现在以下几个方面:

1.python3中print是一个内置函数,有多个参数,而python2中print是一个语法结构;

2.Python2打印时可以不加括号:print ‘hello world‘, Python3则需要加括号   print("hello world")

3.Python2中,input要求输入的字符串必须要加引号,为了避免读取非字符串类型发生的一些行为,不得不使用raw_input()代替input()

 

1. python3中,或许开发者觉得print同时具有两重身份有些不爽,就只留了其中函数的身份:

技术分享
print(value1, ..., sep= , end=\n, file=sys.stdout, flush=False)
View Code

从上面的方法原型可以看出,

①. print可以支持多个参数,支持同时打印多个字符串(其中...表示任意多个字符串);

②. sep表示多个字符串之间使用什么字符连接;

③. end表示字符串结尾添加什么字符,指点该参数就可以轻松设置打印不换行,Python2.x下的print语句在输出字符串之后会默认换行,如果不希望换行,只要在语句最后加一个“,”即可。但是在Python 3.x下,print()变成内置函数,加“,”的老方法就行不通了。

1 >>> print("python", "tab", ".com", sep=‘‘)  
2 pythontab.com  
3 >>>  print("python", "tab", ".com", sep=‘‘, end=‘‘) #就可以实现打印出来不换行  
4 pythontab.com  

Python2打印时可以不加括号:print ‘hello world‘, Python3则需要加括号   print("hello world")

python3中print必须使用括号,因为它就是一个函数。

Python2和Python3中print的不同点

标签:参数   hello   close   std   sed   hid   必须   换行   splay   

原文地址:http://www.cnblogs.com/saolv/p/7807792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!