大家應(yīng)該知道python中print之后是默認換行的,
那如何我們不想換行,且不想講輸出內(nèi)容用一個print函數(shù)輸出時,就需要改變print默認換行的屬性,
方法如下:
print('contents',end='!@#$%^&*')
end就表示print將如何結(jié)束,默認為end="\n"(換行)
栗子:
print("祝各位身體健康")
print("!")
print("祝各位身體健康",end='')
print("!")
python3.0的print函數(shù)有如下的形式:
print([object,...][,seq=''][,end='\n'][,file=sys.stdout])
我們在使用print()函數(shù)時,并不希望輸出結(jié)束后自動換行,因此,我們可以按照下面的方法來做
1.print()指定結(jié)束符
print('hello',end='')
print('world')
#result:helloworld
當print()函數(shù),指定end參數(shù)為空字符后,print()函數(shù)就不再主動添加換行符了。并且,hello和world之間也不存在任何空格。
a='firstline'
b='secondline'
c='thirdline'
print(a,end='\n\n')
print(b)
print(c,end='!')
我們可以利用指定結(jié)束符的方法,靈活控制換行行數(shù)和結(jié)尾字符。
以上內(nèi)容為大家介紹了python培訓之print如何不換行,希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓機構(gòu):千鋒教育。http://www.jsszjs.cn/