Python print函数,python如何实现print不换行
前言我最近在看《python网络爬虫实战》。 里面的第三章有印99乘法表的例子,书的代码是基于Python2.X的,所以在我的电脑上是Python3.6。 结果,print () )的时候出现了问题,所以通过写博客进行了记录。
python2.X版本的python2.X代码如下:
classprinttable(object ) : def _ init _ (self ) : print ) ) 9X9乘法表打印开始) ) self.print99 ) defprint99 ) self 10
虽然是Python3.6版本,但Python3.6版本的输出如下所示。
很明显print ) )函数会自动换行。 2 .为了实现类似x的效果,需要将代码改为
classprinttable(object ) : def _ init _ (self ) : print ) ) 9X9乘法表打印开始) ) self.print99 ) defprint99 ) self 10
print (去除了函数)、“附加end=”说明了为什么要这样变更。 这才是本文的重点,我们不仅需要知道其理由,也需要知道其理由。 原因是Python3对print ()进行了更改
1.print从语句变为函数python2. x : print‘hello’,‘world’
python3. x :打印(“hello”、“world”) )。
既然是函数,print ()就有一个名为end=)的参数,默认值为“\ n”,因此默认情况下将执行换行操作。 如上例所示,要避免换行,只能指定end=)。
举个例子:
未指定结束参数:
打印(hello ) (打印) )的输出结果如下。
指定了end=
print('hello ',end=' ' ) print('world ' )的输出结果如下:
2.xrange被移除了python2. x : foriinxrange (1,10 )
python3. x : foriinrange (1,10 ) )。
xrange的实现已经包含在range中
总结一下目前的趋势,Python3无疑是大势所趋,而且Python2版本也将在2020年停止维护,所以建议学习python时尽量选择基于Python3的教程和资料。
这里有一个关于python2.7停止维护倒计时的网站
Python 2,thankyouforyouryearsoffaithfulservice。
Python 3,your time is now。