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 相关文章推荐
在Python的web框架中编写创建日志的程序的教程
Apr 30 Python
开始着手第一个Django项目
Jul 15 Python
利用Python实现图书超期提醒
Aug 02 Python
TensorFlow变量管理详解
Mar 10 Python
python3爬虫怎样构建请求header
Dec 23 Python
numpy基础教程之np.linalg
Feb 12 Python
Python with关键字,上下文管理器,@contextmanager文件操作示例
Oct 17 Python
Python中Subprocess的不同函数解析
Dec 10 Python
keras模型可视化,层可视化及kernel可视化实例
Jan 24 Python
Python多线程实现支付模拟请求过程解析
Apr 21 Python
Python调用.net动态库实现过程解析
Jun 05 Python
Django框架中表单的用法
Jun 10 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管理内存函数 memory_get_usage()使用介绍
2012/09/23 PHP
腾讯QQ微博API接口获取微博内容
2013/10/30 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
2017/07/26 PHP
PHP基于自定义函数实现的汉字转拼音功能实例
2017/09/30 PHP
什么是PHP7中的孤儿进程与僵尸进程
2019/04/14 PHP
使用JQuery和s3captche实现一个水果名字的验证
2009/08/14 Javascript
jQuery lazyload 的重复加载错误以及修复方法
2010/11/19 Javascript
jquery的index方法实现tab效果
2011/02/16 Javascript
JS格式化数字保留两位小数点示例代码
2013/10/15 Javascript
页面实时更新时间的JS实例代码
2013/12/18 Javascript
javascript检查浏览器是否已经启用XX功能
2015/07/10 Javascript
jQuery插件zTree实现删除树节点的方法示例
2017/03/08 Javascript
JavaScript递归算法生成树形菜单
2017/08/15 Javascript
JS与jQuery判断文本框还剩多少字符可以输入的方法
2018/09/01 jQuery
浅谈高大上的微信小程序中渲染html内容—技术分享
2018/10/25 Javascript
基于游标的分页接口实现代码示例
2018/11/12 Javascript
elementUI 动态生成几行几列的方法示例
2019/07/11 Javascript
layui按条件隐藏表格列的实例
2019/09/19 Javascript
[01:54]TI珍贵瞬间系列(五):压力
2020/08/29 DOTA
跟老齐学Python之for循环语句
2014/10/02 Python
python使用xpath中遇到:到底是什么?
2018/01/04 Python
python散点图实例之随机漫步
2018/08/27 Python
python异步存储数据详解
2019/03/19 Python
浅谈Python的条件判断语句if/else语句
2019/03/21 Python
基于Python组装jmx并调用JMeter实现压力测试
2020/11/03 Python
python中Array和DataFrame相互转换的实例讲解
2021/02/03 Python
德国珠宝和手表在线商店:VALMANO
2019/03/24 全球购物
乌克兰数字设备、配件和智能技术的连锁商店:KTC
2020/08/18 全球购物
5个HTML5的常用本地存储方式详解与介绍
2021/03/27 HTML / CSS
服装设计专业自荐书范文
2013/12/30 职场文书
学习十八大演讲稿
2014/09/15 职场文书
《灰雀》教学反思
2016/02/19 职场文书
jQuery class属性操作addClass()与removeClass()、hasClass()、toggleClass()
2021/03/31 jQuery
JavaCV实现照片马赛克效果
2022/01/22 Java/Android
千万级用户系统SQL调优实战分享
2022/03/03 MySQL
Python的property属性详细讲解
2022/04/11 Python