在Python中,print()
函數(shù)主要用于在控制臺上輸出文本。它可以接受多種類型的參數(shù),如字符串、整數(shù)、浮點數(shù)等,并將它們轉(zhuǎn)換為字符串以便在屏幕上顯示。以下是一些常見的使用方法:
print("Hello, World!")
name = "Alice"
age = 30
print("My name is", name, "and I am", age, "years old.")
name = "Bob"
age = 25
print(f"My name is {name} and I am {age} years old.")
my_list = [1, 2, 3]
my_tuple = (4, 5, 6)
my_dict = {"key1": "value1", "key2": "value2"}
print("List:", my_list)
print("Tuple:", my_tuple)
print("Dictionary:", my_dict)
print("apple", "banana", "cherry", sep="-", end="!\n")
這將輸出:apple-banana-cherry!
with open("output.txt", "w") as file:
print("Hello, World!", file=file)
這將把"Hello, World!"寫入名為"output.txt"的文件中。
這只是print()
函數(shù)的一些基本功能。通過組合不同的參數(shù)和格式化選項,你可以根據(jù)需要定制輸出內(nèi)容。