Python print 函式中的 sep 引數
Muhammad Waiz Khan
2023年10月10日

本教程將解釋 print()
函式中的 sep
引數。
print(objects, sep, end, file, flush)
函式將 objects
轉換成字串並將它們列印到提供的 file
文字流中; end
是最後輸入 object
末尾附加的值。如果使用者要強制重新整理文字流,則將 flush
設定為 True
。sep
引數用作分隔符,分隔由 print()
函式列印的 objects
。
Python 中 print()
函式的 sep
引數
sep
引數的預設值為' '
,這就是為什麼多個字串由 print()
函式列印並由' '
分隔的原因。下面的示例程式碼演示了使用 print()
函式時 sep
引數的預設值的輸出:
print('hey','hi','hello')
輸出:
hey hi hello
我們可以自己設定 sep
引數的值,並使用所選的 sep
值分隔輸入的 objects
(我們要列印的)。下面的示例程式碼演示瞭如何在 Python 中使用不同的值作為 print()
函式的 sep
引數。
print("hey", "hi", "hello", sep="_")
print("hey", "hi", "hello", sep="\t")
print("hey", "hi", "hello", sep="\n")
輸出:
hey_hi_hello
hey hi hello
hey
hi
hello