在python中我們偶爾會(huì)用到輸出不換行的效果,python2中使用逗號(hào),即可,而python3中使用end=''來(lái)實(shí)現(xiàn)的,這里簡(jiǎn)單為大家介紹一下,需要的朋友可以參考下
python輸出不換行
Python2的寫(xiě)法是:
print 'hello',
Python3的寫(xiě)法是:
print('hello', end='')對(duì)于python2和python3都兼容的寫(xiě)法是:
from __future__ import print_functionprint('hello', end='') python ,end=''備注
就是打印之后不換行。在Python2.7中使用“,”
下面是2.7的例子:
def test():print 'hello',print 'world'
輸出 hello world
hello后面沒(méi)有換行。
如果是python3以后的版本中則用end=‘ '
在python3.x之后,可以在print()之中加end=""來(lái)解決,可以自定義結(jié)尾字符
print ('hello',end= ' ')print ('world')end后面的內(nèi)容就是一個(gè)空格,要不hello world就變成helloworld了。
繼續(xù)看下面的實(shí)例
'end='意思是不換行,例如:temp = input('輸入一個(gè)整數(shù)')i = int(temp)while i : print('*') i = i - 1輸入4結(jié)果是:****更改代碼:temp = input('輸入一個(gè)整數(shù)')i = int(temp)while i : print('*',end = '') i = i - 1輸入4結(jié)果是:****
新聞熱點(diǎn)
疑難解答
圖片精選