Python中提供了多種格式化輸出的方法,其中最常用的是使用format方法和f-string。
format方法:name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
f-string(Python 3.6及以上版本):name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
以上兩種方法都可以在字符串中使用占位符(如{})來表示要格式化輸出的變量,然后在format方法的參數或f-string中使用相應的變量值來替換占位符。