日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

[转载] python[1]-print中的sep、end参数

發(fā)布時(shí)間:2025/3/11 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载] python[1]-print中的sep、end参数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考鏈接: Python | print()中的sep參數(shù)

讀示例程序代碼時(shí)遇到的問題,看不懂end和sep參數(shù)。經(jīng)過查找,基本弄清楚了。 sep:可以設(shè)置print中分割不同值的形式。應(yīng)該是separation的縮寫。 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

?

可以看到,沒有參數(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é)尾,并沒有默認(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ù)沒有設(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参数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。