[转载] python[1]-print中的sep、end参数
參考鏈接: Python | print()中的sep參數(shù)
讀示例程序代碼時(shí)遇到的問(wèn)題,看不懂end和sep參數(shù)。經(jīng)過(guò)查找,基本弄清楚了。 sep:可以設(shè)置print中分割不同值的形式。應(yīng)該是separation的縮寫(xiě)。 end:可以設(shè)置print打印結(jié)束時(shí)最后跟的字符形式。 具體看程序示例。?
示例1,常規(guī)print輸出?
shoplist=['apple','mango','carrot','banana']
print("This is printed without 'end'and 'sep'.")
for item in shoplist:
? ? print(item)
?
輸出結(jié)果:?
This is printed without 'end'and 'sep'.
apple
mango
carrot
banana
?
可以看到,沒(méi)有參數(shù)時(shí),每次輸出后都會(huì)換行。 2. 示例2,設(shè)置end參數(shù)輸出。?
shoplist=['apple','mango','carrot','banana']
print("This is printed with 'end='$''.")
for item in shoplist:
? ? print(item, end='$')
print('hello world')
?
輸出結(jié)果:?
This is printed with 'end='$''.
apple$mango$carrot$banana$hello world
?
可以看到每次輸出結(jié)束都用end設(shè)置的參數(shù)$結(jié)尾,并沒(méi)有默認(rèn)換行。 3. 示例3,設(shè)置sep參數(shù)輸出。?
print("This is printed with 'sep='&''.")
for item in shoplist:
? ? print(item,'another string',sep='&')
?
輸出結(jié)果:?
This is printed with 'sep='&''.
apple&another string
mango&another string
carrot&another string
banana&another string
?
可以看到,item值與‘a(chǎn)nother string’兩個(gè)值之間用sep設(shè)置的參數(shù)&分割。由于end參數(shù)沒(méi)有設(shè)置,因此默認(rèn)是輸出解釋后換行,即end參數(shù)的默認(rèn)值為\n(轉(zhuǎn)義-換行符)。?
參考文章:?
[1] :https://www.cnblogs.com/owasp/p/5372476.html [2] : book:byte of python
總結(jié)
以上是生活随笔為你收集整理的[转载] python[1]-print中的sep、end参数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java enummap_Java En
- 下一篇: 操作系统中的死锁_操作系统中的死锁介绍