Python 最近出了个大新闻:PEP-750 t-string 语法被正式采纳了!这意味着 Python 将在今年 10 月发布的 3.14 版本中引入一种新的字符串前缀t,称为模板字符串(Template Strings),即 t-string。这是继 f-string 之后,字符串处理能力的重大升级,旨在提供更安全、更灵活的字符串插值处理机制。t-string 的基本语法与 f-string 非常相似,但得到的结果却大为不同:name="World"# f-string 语法formatted=f"Hello{name}!"print(type(formatted))# 输出:print(formatted)# 输出:Hello World!# t-string 语法templated=t"Hello {name}!"print(type(templated))# 输出:print(templated.strings)# 输出:('Hello ', '')print(templated.interpolations[0].value)# 输出:Worldprint("".join(itemifisinstance(item,str)elsestr(item.value)foritemintemplated))# 输出:Hello World!如上所述,t-string 与 f
...
继续阅读
(44)