參考鏈接: Python | print()中的sep參數
讀示例程序代碼時遇到的問題,看不懂end和sep參數。經過查找,基本弄清楚了。 sep:可以設置print中分割不同值的形式。應該是separation的縮寫。 end:可以設置print打印結束時最后跟的字符形式。 具體看程序示例。?
示例1,常規print輸出?
shoplist=['apple','mango','carrot','banana']
print("This is printed without 'end'and 'sep'.")
for item in shoplist:
? ? print(item)
?
輸出結果:?
This is printed without 'end'and 'sep'.
apple
mango
carrot
banana
?
可以看到,沒有參數時,每次輸出后都會換行。 2. 示例2,設置end參數輸出。?
shoplist=['apple','mango','carrot','banana']
print("This is printed with 'end='$''.")
for item in shoplist:
? ? print(item, end='$')
print('hello world')
?
輸出結果:?
This is printed with 'end='$''.
apple$mango$carrot$banana$hello world
?
可以看到每次輸出結束都用end設置的參數$結尾,并沒有默認換行。 3. 示例3,設置sep參數輸出。?
print("This is printed with 'sep='&''.")
for item in shoplist:
? ? print(item,'another string',sep='&')
?
輸出結果:?
This is printed with 'sep='&''.
apple&another string
mango&another string
carrot&another string
banana&another string
?
可以看到,item值與‘another string’兩個值之間用sep設置的參數&分割。由于end參數沒有設置,因此默認是輸出解釋后換行,即end參數的默認值為\n(轉義-換行符)。?
參考文章:?
[1] :https://www.cnblogs.com/owasp/p/5372476.html [2] : book:byte of python