【学习笔记】python2的print和python3的print()
python2.x和3.x中的输出语句有着明显不同
2.x中的print不是个函数,输出格式如下
Python 2.7.12+ (default, Aug 4 2016, 20:04:34)
[GCC 6.1.1 20160724] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "There is only %d %s in the sky."%(1,'sun')
There is only 1 sun in the sky.
3.x中的print成了函数,输出格式如下
Python 3.5.2+ (default, Aug 5 2016, 08:07:14)
[GCC 6.1.1 20160724] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("There is only %d %s in the sky."%(1,'sun'))
There is only 1 sun in the sky.
为什么要做出这样的变化,主要原因有以下几点:
1.print不是函数,不能使用help(),对使用者不方便。
python2中help(print)会报错。
>>> help(print)
File "<stdin>", line 1
help(print)
^
SyntaxError: invalid syntax
python3中,可以使用help(print),清楚的看到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.
(END)
2.从上面的help(print)中我们也可以看到在print()中的两个重要参数,sep和end。这两个参数使print()相比print多了两个新功能,自定义间隔符(默认空格)和结束符(默认回车)。
>>> print("","","")
123 456 789
>>> print("","","",sep='-')
123-456-789
>>> x=1024
>>> print(t)
256
>>> print(t,end=" end")
256 end>>>
>>> print(t,end=" end\n")
256 end
3.print()重定向输出文件更加方便。
2.x需要print>>重定向输出,感觉代码很混乱。
>>> out=open("test.txt","w")
>>> print>>out,""
3.x中输出文件成了一个参数,使用更方便。
>>> out=open("test.txt","w")
>>> print("",file=out)
4.python2.x中print语句的格式化输出源自于C语言的格式化输出,这种语法对于C这种静态语言比较适用,但是对于拥有很多先进数据结构的python来说就有点力不从心了。python的元组,列表,字典,集合等不适合用这种结构表示,这些数据结构大多元素用下标表示,在这种结构中写出来很混乱。python3.x的print()函数提供了有点类似C#(不知道这么说对不对)中的格式化输出函数format()。另外print()也兼容原来的格式化输出方式。
>>> print("%s is %s."%('Aoko','good'))
Aoko is good.
format()让输出格式更清晰。
>>> print("{0} is {1}.".format('Aoko','good'))
Aoko is good.
format()支持数组下标,使python中的一些数据结构输出更加方便。
>>> name=["Kaito",5]
>>> print("{0[0]} has {0[1]} dollars.".format(name))
Kaito has 5 dollars.
format()下的格式限定符,和原来的差不多。
>>> x=5.6
>>> print("{0:4f}".format(x))
5.600000
由此看来,print()相比print还是有很大进步的。说句题外话,我希望更多的python用户多花点时间实现代码对新版本的兼容,而不是花时间用在争论“python2和python3谁更好”的口水战上。python作为一种免费语言给我们带来了很多方便,我们不应该吝惜自己那么一点时间。花一点时间让python发展下去,变得更强。
【学习笔记】python2的print和python3的print()的更多相关文章
- 【学习笔记】第七章 python3核心技术与实践--输入与输出
[第六章]思考题答案,仅供参考: # coding:utf-8import time#方法一start_time = time.perf_counter()s = ''for n in range(0 ...
- 学习笔记之X分钟速成Python3
X分钟速成Python3 https://mp.weixin.qq.com/s/QT5sR0nUKgJYsYgrj2SleA https://learnxinyminutes.com/docs/zh- ...
- 【学习笔记】第五章 python3核心技术与实践--字典和集合
[第四章]思考题的答案,仅供参考: []比list()更快,因为调用了list函数有一定的时间,而[]却没有. 前面我们学习了 Python 中的列表和元组,了解了他们的基本操作和性能比较.这节章,我 ...
- python3.5学习笔记:linux6.4 安装python3 pip setuptools
前言: python3应该是python的趋势所在,当然目前争议也比较大,这篇随笔的主要目的是记录在linux6.4下搭建python3环境的过程 以及碰到的问题和解决过程. 另外,如果本机安装了py ...
- Python学习笔记之Centos6.9安装Python3.6
0x00 注意 如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如yum!!!!! 不要动现有的python2环 ...
- 【学习笔记】第四章 Python3核心技术与实践--列表与元组
前面的课程,我们了解了Python 语言的学习方法,并且带你了解了 Python 必知的常用工具——Jupyter.接下来我们正式学习 Python 的具体知识. 对于每一门编程语言来说,数据结构都是 ...
- (学习笔记)PHP的输出echo和print
echo 和 print 之间的差异: echo - 能够输出一个以上的字符串 print - 只能输出一个字符串,并始终返回 1 echo语句 echo 或 echo()均可. 输出换行 echo ...
- 【学习笔记】第三章 python3核心技术与实践--Jupyter Notebook
可能你已经知道,Python 在 14 年后的“崛起”,得益于机器学习和数学统计应用的兴起.那为什么 Python 如此适合数学统计和机器学习呢?作为“老司机”的我可以肯定地告诉你,Jupyter N ...
- Python3学习笔记01-环境安装和运行环境
最近在学习Python3,想写一些自己的学习笔记.方便自己以后看,主要学习的资料来自菜鸟教程的Python3教程和廖雪峰官方网站的Python教程. 1.下载 1)打开https://www.pyth ...
随机推荐
- python多线程爬取-今日头条的街拍数据(附源码加思路注释)
这里用的是json+re+requests+beautifulsoup+多线程 1 import json import re from multiprocessing.pool import Poo ...
- Ajax增删改查-----------增
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [转] 对Array.prototype.slice.call()方法的理解
在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢? 1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规 ...
- ionic 3 安卓手机获取经纬度坐标
现在有个需求:每隔一段时间需向后台服务器返回当前用户的经纬度坐标. ionic 官方提供的有定位插件cordova-plugin-geolocation,兼容ios和android版本,网上查资料说最 ...
- 3897: Power
题解: 首先很贪心的选择 有最大的我们一定会用最大的 然后可以将序列分割.. 就变成了一道模拟题了.. 每个状态记录(h,t,h-have,t-need) 注意一下细节就可以了 代码: #includ ...
- EF 数据版本号,处理具体使用方法 RowVersion / Timestamp 使用方法。进行自动处理并发修改
/* * <div class="form-group"> // 原始 * <div class="form-group hidden"> ...
- Js计算时间差,天数,小时数,余数
var begintime_ms = Date.parse(new Date(begintime.replace(/-/g, "/"))); //begintime 为开始时间 v ...
- train_test_split数据切分
train_test_split 数据切分 格式: X_train,X_test, y_train, y_test =cross_validation.train_test_split(train_d ...
- jmeter访问mysql数据库
jdbc:mysql://localhost:3306/jy?allowMultiQueries=true 如果想同时执行多条语句
- appium---第四个脚本,进入app,有权限弹窗的方法
1.以淘宝为例:进入首页,会弹出好几个权限弹窗 无法使用id定位 用xpath定位