在Python中,print函數可以使用格式化字符串來實現格式化輸出。以下是幾種常見的格式化輸出方法:
name = "Alice"
age = 25
height = 1.65
print("My name is %s, I am %d years old and my height is %.2f meters." % (name, age, height))
name = "Alice"
age = 25
height = 1.65
print("My name is {}, I am {} years old and my height is {:.2f} meters.".format(name, age, height))
name = "Alice"
age = 25
height = 1.65
print(f"My name is {name}, I am {age} years old and my height is {height:.2f} meters.")
這些方法可以使輸出更加直觀和易讀。您可以根據自己的喜好和需求選擇其中的一種。