在Python中,print()
函數(shù)是一個(gè)內(nèi)置函數(shù),用于在控制臺(tái)上輸出文本。要實(shí)現(xiàn)print
的多重功能,您可以使用不同的參數(shù)和選項(xiàng)。以下是一些示例:
print("Hello, World!")
x = 5
y = 10
print("The values of x and y are:", x, y)
name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")
sep
參數(shù)更改分隔符:print("apple", "banana", "cherry", sep=", ")
end
參數(shù)更改行尾字符:print("This is a test.", end="***\n")
with open("output.txt", "w") as file:
print("Hello, World!", file=file)
print()
函數(shù)的返回值(默認(rèn)為None
):result = print("Hello, World!")
print("The return value of print() is:", result)
這只是print()
函數(shù)的一些功能。您可以根據(jù)需要組合這些功能以實(shí)現(xiàn)所需的輸出效果。