python2 与python3的print区别小结


Posted in Python onJanuary 16, 2018

在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)

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

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

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

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

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

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

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

总地来说, Python2.7的print不是一个function,而Python3里的print是一个function。
两都调用方式的主要区别如下:

print 'this is a string' #python2.7
print('this is a string') #python3

当然,python2.7里你也可以用括号把变量括起来, 一点都不会错:

print('this is a string') #python2.7

但是python3将print改成function不是白给的:

1. 在python3里,能使用help(print)查看它的文档了, 而python2不行:

>>help(print)
Help on built-in function print in module builtins:

print(...)
 print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

 Prints the values to a stream, or to sys.stdout by default.
 Optional keyword arguments:
 file: a file-like object (stream); defaults to the current sys.stdout.
 sep: string inserted between values, default a space.
 end: string appended after the last value, default a newline.
 flush: whether to forcibly flush the stream.

2 . 在python3里,能更方便的使用输出重定向

python2.7里,你需要以类似于C++的风格完成重定向:

with open('print.txt', 'w') as f:
 print >> f, 'hello, python!'

在python3里:

with open('print.txt', 'w') as f:
 print('hello, python!', file = f)

file是python3 print新加的一个参数。 另一个很handy的参数是sep, 例如打印一个整数数组, 但你想用星号而不是空格连接。python2时可能需要写一个循环来完成, python3里这样就行了:

a = [1, 2, 3, 4, 5]
print(*a, sep = '*')

最后, 如果想在python2.7里使用python3的print,只需要在第一句代码前加入:

from __future__ import print_function

注意, from __future__ import ...一类的语句一定要放在代码开始处。

print输出差异:同一段代码

#/usr/bin/env python
#coding:utf-8
for i in range(1,10):
    for j in range(1,10):
        for k in range(1,10):
            if(i != k)and(i != j)and(k != j):
                print(i,j,k)

pyhon2的输出为 i,j,k
python3的输出为 i j k
python3的输出直接屏蔽了逗号。

另python2 的print后序可不添加括号。
phthon3必须添加括号。

Python 相关文章推荐
在Python3中初学者应会的一些基本的提升效率的小技巧
Mar 31 Python
python实现从ftp服务器下载文件的方法
Apr 30 Python
Python中的localtime()方法使用详解
May 22 Python
python从入门到精通(DAY 2)
Dec 20 Python
Python使用正则表达式抓取网页图片的方法示例
Apr 21 Python
django站点管理详解
Dec 12 Python
mac安装scrapy并创建项目的实例讲解
Jun 13 Python
浅谈python下含中文字符串正则表达式的编码问题
Dec 07 Python
利用anaconda保证64位和32位的python共存
Mar 09 Python
tensorflow 模型权重导出实例
Jan 24 Python
pycharm sciview的图片另存为操作
Jun 01 Python
Python中tqdm的使用和例子
Sep 23 Python
python正则中最短匹配实现代码
Jan 16 #Python
Python程序员面试题 你必须提前准备!
Jan 16 #Python
详解python使用递归、尾递归、循环三种方式实现斐波那契数列
Jan 16 #Python
Python+tkinter模拟“记住我”自动登录实例代码
Jan 16 #Python
Python利用字典将两个通讯录文本合并为一个文本实例
Jan 16 #Python
Python爬虫爬取一个网页上的图片地址实例代码
Jan 16 #Python
Python+Turtle动态绘制一棵树实例分享
Jan 16 #Python
You might like
PHP实现的交通银行网银在线支付接口ECSHOP插件和使用例子
2014/05/10 PHP
PHP面向对象编程之深入理解方法重载与方法覆盖(多态)
2015/12/24 PHP
WordPress开发中短代码的实现及相关函数使用技巧
2016/01/05 PHP
Nigma vs Alliance BO5 第三场2.14
2021/03/10 DOTA
jQuery性能优化的38个建议
2014/03/04 Javascript
js选择并转移导航菜单示例代码
2014/08/19 Javascript
jQuery EasyUI实现右键菜单变灰不可用效果
2015/09/24 Javascript
AngularJS中比较两个数组是否相同
2016/08/24 Javascript
vuejs动态组件给子组件传递数据的方法详解
2016/09/09 Javascript
jquery hover 不停闪动问题的解决方法(亦为stop()的使用)
2017/02/10 Javascript
js中apply()和call()的区别与用法实例分析
2018/08/14 Javascript
详解离线安装npm包的几种方法
2018/11/25 Javascript
vue 关闭浏览器窗口的时候,清空localStorage的数据示例
2019/11/06 Javascript
vue限制输入框只能输入8位整数和2位小数的代码
2019/11/06 Javascript
[02:02]特效爆炸!DOTA2珍宝之瓶待你开启
2018/08/21 DOTA
用python实现批量重命名文件的代码
2012/05/25 Python
浅谈Python中的数据类型
2015/05/05 Python
Python基于ThreadingTCPServer创建多线程代理的方法示例
2018/01/11 Python
python使用Matplotlib画条形图
2020/03/25 Python
numpy concatenate数组拼接方法示例介绍
2019/05/27 Python
Python微信操控itchat的方法
2019/05/31 Python
Python OrderedDict的使用案例解析
2019/10/25 Python
Python 支持向量机分类器的实现
2020/01/15 Python
波兰家居和花园家具专家:4Home
2019/05/26 全球购物
运动鞋、足球鞋和慕尼黑球衣:Sport Münzinger
2019/08/26 全球购物
彪马法国官网:PUMA法国
2019/12/15 全球购物
Derek Rose官网:英国高档睡衣、家居服和内衣品牌
2020/01/18 全球购物
经理秘书岗位职责
2013/11/14 职场文书
大学学习计划书范文
2014/05/02 职场文书
幼儿园秋季开学通知
2015/07/16 职场文书
运动会运动员赞词
2015/07/22 职场文书
微信早安问候语
2015/11/10 职场文书
《莫泊桑拜师》教学反思
2016/02/22 职场文书
jQuery class属性操作addClass()与removeClass()、hasClass()、toggleClass()
2021/03/31 jQuery
mysql数据库隔离级别详解
2022/06/16 MySQL
腾讯云服务器部署前后分离项目之前端部署
2022/06/28 Servers