iT邦幫忙

2025 iThome 鐵人賽

DAY 26
0
自我挑戰組

c 語言與 python 的30天之旅系列 第 26

Python 3.14 新語法

  • 分享至 

  • xImage
  •  

模板字串 (T-strings)
受到 JavaScript 標記模板文字的啟發,Python 的新模板字串(暱稱為 t-strings)。從語法上講,t 字串與 f 字串非常相似,共享相同的佔位符語法、可選的格式說明符和轉換標誌。乍一看,唯一可見的差異是前置詞,因為範本字串常值以 t 或 T 開頭。
然而,與 f-strings 不同,t-strings 計算結果為一個新的 string.templatelib.Template 物件,而不是直接生成一個普通的 str:

name = "Alice"
age = 30
template = t"Hello, {name}! You are {age} years old."

上一篇
Python 與 Lambda 函式
下一篇
Python 與 C 延伸模組(extension modules)
系列文
c 語言與 python 的30天之旅27
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言