2017.7.5(转)—【学习笔记】python2的print和python3的print()
来源:互联网 发布:推荐好用的精华液知乎 编辑:程序博客网 时间:2024/06/07 00:58
python2.x和3.x中的输出语句有着明显不同
2.x中的print不是个函数,输出格式如下
1 Python 2.7.12+ (default, Aug 4 2016, 20:04:34) 2 [GCC 6.1.1 20160724] on linux23 Type "help", "copyright", "credits" or "license" for more information.4 >>> print "There is only %d %s in the sky."%(1,'sun')5 There is only 1 sun in the sky.
3.x中的print成了函数,输出格式如下
1 Python 3.5.2+ (default, Aug 5 2016, 08:07:14) 2 [GCC 6.1.1 20160724] on linux3 Type "help", "copyright", "credits" or "license" for more information.4 >>> print("There is only %d %s in the sky."%(1,'sun'))5 There is only 1 sun in the sky.
为什么要做出这样的变化,主要原因有以下几点:
1.print不是函数,不能使用help(),对使用者不方便。
python2中help(print)会报错。
1 >>> help(print)2 File "<stdin>", line 13 help(print)4 ^5 SyntaxError: invalid syntax
python3中,可以使用help(print),清楚的看到print的参数。
1 Help on built-in function print in module builtins: 2 3 print(...) 4 print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) 5 6 Prints the values to a stream, or to sys.stdout by default. 7 Optional keyword arguments: 8 file: a file-like object (stream); defaults to the current sys.stdout. 9 sep: string inserted between values, default a space.10 end: string appended after the last value, default a newline.11 flush: whether to forcibly flush the stream.12 (END)
2.从上面的help(print)中我们也可以看到在print()中的两个重要参数,sep和end。这两个参数使print()相比print多了两个新功能,自定义间隔符(默认空格)和结束符(默认回车)。
1 >>> print("123","456","789")2 123 456 7893 >>> print("123","456","789",sep='-')4 123-456-7891 >>> x=10242 >>> print(t)3 2564 >>> print(t,end=" end")5 256 end>>> 6 >>> print(t,end=" end\n")7 256 end
3.print()重定向输出文件更加方便。
2.x需要print>>重定向输出,感觉代码很混乱。
1 >>> out=open("test.txt","w")2 >>> print>>out,"123"
3.x中输出文件成了一个参数,使用更方便。
1 >>> out=open("test.txt","w")2 >>> print("123",file=out)
4.python2.x中print语句的格式化输出源自于C语言的格式化输出,这种语法对于C这种静态语言比较适用,但是对于拥有很多先进数据结构的python来说就有点力不从心了。python的元组,列表,字典,集合等不适合用这种结构表示,这些数据结构大多元素用下标表示,在这种结构中写出来很混乱。python3.x的print()函数提供了有点类似C#(不知道这么说对不对)中的格式化输出函数format()。另外print()也兼容原来的格式化输出方式。
1 >>> print("%s is %s."%('Aoko','good'))2 Aoko is good.
format()让输出格式更清晰。
1 >>> print("{0} is {1}.".format('Aoko','good'))2 Aoko is good.
format()支持数组下标,使python中的一些数据结构输出更加方便。
1 >>> name=["Kaito",5]2 >>> print("{0[0]} has {0[1]} dollars.".format(name))3 Kaito has 5 dollars.
format()下的格式限定符,和原来的差不多。
1 >>> x=5.62 >>> print("{0:4f}".format(x))3 5.600000
由此看来,print()相比print还是有很大进步的。说句题外话,我希望更多的python用户多花点时间实现代码对新版本的兼容,而不是花时间用在争论“python2和python3谁更好”的口水战上。python作为一种免费语言给我们带来了很多方便,我们不应该吝惜自己那么一点时间。花一点时间让python发展下去,变得更强。
老师说可以用python3了,疲于解决2和3之间的差异,主要是进度跟不上。随后将使用3去了
- 2017.7.5(转)—【学习笔记】python2的print和python3的print()
- python2的print和python3的print()
- Python2和Python3的print
- Python2与Python3的区别(三):print
- python2 与python3的print差异
- python3和python2中print的用法有很多不同,python3中需要使用括号
- python2.7与python3中print的一点不同之处
- python2与python3的print及字符串格式化总结
- Python3中的print函数以及与python2的比较
- Python3与Python2中print的用法改变与Class获取属性学习
- (学习笔记)PHP的输出echo和print
- Python3.5里print()的用法
- Python2&Python3 print 不换行
- 2017.7.5(转)python2.7,一个困扰了我一天的print问题
- 【Python】学习笔记——-Python2 和 Python3的区别
- python2.7中print()的使用
- 注意Python2.x与Python3.x中的print与print()用法(invalid syntax错误)
- Python中print和字符格式化的小结(Python2.7.5)
- ORACLE 10g 第一课 绪论 安装及体系结构概述
- setInterval和setTimeOut
- 微信样式库使用
- Cocos2d-x 3.x lua中的定时器
- JavaWeb中的Servlet概述
- 2017.7.5(转)—【学习笔记】python2的print和python3的print()
- 粒子系统参数详情
- Ajax 、编码问题
- 实验报告->数组的定义和使用->冒泡排序和选择排序
- iOS转前端之HTML基础知识学习
- mac下java ee 1.7下载安装
- jsp/servlet上传文件
- logstash使用webhdfs插件指定输出字段存储数据到hdfs时间分层(还能保留原来数据)
- 将jdk版本:jdk1.8更换为jdk1.7之后输入java -version还是出现1.8的版本号