共計(jì) 315 個(gè)字符,預(yù)計(jì)需要花費(fèi) 1 分鐘才能閱讀完成。
在 Python 中,可以使用字符串的 format()
方法或者 f -string 來插入變量的值到字符串中。
使用 format()
方法:
name = "Alice"
age = 30
sentence = "My name is {} and I am {} years old.".format(name, age)
print(sentence)
使用 f -string:
name = "Alice"
age = 30
sentence = f"My name is {name} and I am {age} years old."
print(sentence)
這兩種方法都可以將變量的值插入到字符串中。f-string 是 Python 3.6 及以上版本引入的新特性,更簡(jiǎn)潔方便。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完