V2EX  ›  英汉词典

F-string

Definition 定义

(编程,尤指 Python)f-string 指“格式化字符串字面量”:在字符串前加上 fF,就可以在字符串中用 {} 直接嵌入变量、表达式,并进行格式化输出(如保留小数位、对齐等)。常见于 Python 3.6 及以上版本。

Pronunciation 发音

/ˈɛf strɪŋ/

Examples 例句

I used an f-string to print her name: f"Hello, {name}!"
我用 f-string 来打印她的名字:f"Hello, {name}!"

With f-strings, you can format numbers neatly, such as f"{price:.2f}", which is clearer than older formatting methods in many cases.
使用 f-string,你可以把数字格式化得很整齐,比如 f"{price:.2f}";在很多情况下,它比旧的格式化方式更清晰。

Etymology 词源

“f-string”中的 f 通常理解为 formatted(“格式化的”)的缩写;这种写法来自 Python 的设计提案与官方术语(即 formatted string literals)。它把“字符串 + 格式化”合在一种更直观的写法里:在字符串前加 f,并用 {} 标记要插入的内容。

Related Words 相关词

Literary Works 文学作品

  • PEP 498 – Literal String Interpolation(Python Enhancement Proposal,提出并规范 f-string 的官方文档)
  • Python Cookbook(David Beazley & Brian K. Jones,常见 Python 写法与技巧中涉及字符串格式化)
  • Fluent Python(Luciano Ramalho,对 Python 习惯用法与表达力的讨论中会涉及字符串格式化与 f-string)
关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   750 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 10ms · UTC 19:12 · PVG 03:12 · LAX 11:12 · JFK 14:12
♥ Do have faith in what you're doing.