python2 与python3的print区别小结

yipeiwu_com5年前Python基础

在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 合并多个格式一致的 Excel 文件(推荐)

使用 Python 合并多个格式一致的 Excel 文件(推荐)

一 问题描述 最近朋友在工作中遇到这样一个问题,她每天都要处理如下一批 Excel 表格:每个表格的都只有一个 sheet,表格的前两行为表格标题及表头,表格的最后一行是相关人员签字。最...

Python判断文本中消息重复次数的方法

本文实例讲述了Python判断文本中消息重复次数的方法。分享给大家供大家参考,具体如下: #coding:gbk ''' Created on 2012-2-3 从文件中读取文本,并...

Python中使用动态变量名的方法

如果要写一个程序,让x1为1,x2为2,然后直到x100为100,你会怎么做? 在C这种静态语言里,变量名这个标识符实际上会被编译器直接翻译成内存地址,所以除了手动设置每个变量的值以外,...

在Python中使用AOP实现Redis缓存示例

越来越觉得的缓存是计算机科学里最NB的发明(没有之一),本文就来介绍了一下在Python中使用AOP实现Redis缓存示例,小伙伴们一起来了解一下 import redis ena...

Python 中的Selenium异常处理实例代码

Python 中的Selenium异常处理实例代码

自动化测试执行过程中,难免会有错误/异常出现,比如测试脚本没有发现对应元素,则会立刻抛出NoSuchElementException异常。这时不要怕,肯定是测试脚本或者测试环境哪里出错了...