Python 2.7与Python3 的print有什么区别?
来源:互联网 发布:如何做合格淘宝客服 编辑:程序博客网 时间:2024/06/05 20:27
总地来说, Python2.7的print不是一个function,而Python3里的print是一个function。
两都调用方式的主要区别如下:
print 'this is a string' #python2.7print('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 ...
一类的语句一定要放在代码开始处。
References
- http://www.cnblogs.com/kaitoex/p/6085606.html
1 0
- Python 2.7与Python3 的print有什么区别?
- Python2与Python3的区别(三):print
- echo,print()和print_r()有什么区别?
- echo ,print,print_r,var_dump有什么区别?
- c++里 p.print() 和 p->print() 有什么区别
- python print 函数与print语句区别
- PrintWriter的print方法,有什么用?
- python2 与python3的print差异
- python的raw_input与print的区别实例
- 在jsp的内值对象中response 和out.print有什么区别啊
- 与有什么区别?
- &与&&有什么区别?
- &与&&有什么区别?
- &与&&有什么区别?
- &与&&有什么区别?
- &与&&有什么区别?
- &与&&有什么区别?
- &与&&有什么区别?
- tessnet2和3版本混用后程序错误的解决
- js中cssText批量修改元素样式
- 百度地图开发(二)--定位,lbs数据查询,路径规划
- Android中判断是否有网络连接以及网络监控状态
- elasticsearch笔记_精确值搜索_filter(四)
- Python 2.7与Python3 的print有什么区别?
- 【OpenCV3】基于双目视觉的三维重建
- C# System.NullReferenceException: 未将对象引用设置到对象的实例
- ProduceQueue;支持堵塞和非堵塞
- 高并发Java 三 Java内存模型和线程安全
- 文件IO编程十一
- sql创建表、删除表、删除字段、增加字段
- iOS开发之蓝牙通讯
- 显卡结构及工作原理详细解读