共計 478 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Python 中,可以使用 print()函數(shù)來打印變量和字符串。
要打印一個變量,只需將變量名放在 print()函數(shù)的括號內即可,例如:
x = 10
print(x)
要同時打印變量和字符串,可以使用逗號將它們分開,例如:
name = "Alice"
age = 25
print("My name is", name, "and I am", age, "years old.")
另一種方法是使用字符串的格式化功能,可以使用占位符來代替變量,然后使用.format()方法將變量填充到字符串中,例如:
name = "Bob"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
或者使用 f -string(格式化字符串字面值),在字符串前加上字母 "f",然后在字符串中使用大括號 {} 來包裹變量名,例如:
name = "Charlie"
age = 35
print(f"My name is {name} and I am {age} years old.")
以上是幾種常用的打印變量和字符串的方法,可以根據(jù)具體情況選擇適合的方法。
丸趣 TV 網(wǎng) – 提供最優(yōu)質的資源集合!
正文完