问题标签 [f-string]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
8 回答
28502 浏览

python - 我可以将 Python 3.6 的格式化字符串文字(f-strings)导入旧的 3.x、2.x Python 吗?

对我来说,新的 Python 3.6 f-strings 似乎是字符串可用性的巨大飞跃,我很想在可能在旧解释器上运行的新项目中全心全意地采用它们。2.7、3.3-3.5 支持会很好,但至少我想在 Python 3.5 代码库中使用这些。如何导入 3.6 的格式化字符串文字以供较旧的解释器使用?

我知道格式化的字符串文字f"Foo is {age} {units} old"不会破坏性更改,因此不会包含在from __future__ import ...调用中。但是更改没有向后移植(AFAIK),我需要确保我用 f-strings 编写的任何新代码都只能在 Python 3.6+ 上运行,这对于很多项目来说都是一个交易破坏者。

0 投票
12 回答
29442 浏览

python - How to postpone/defer the evaluation of f-strings?

I am using template strings to generate some files and I love the conciseness of the new f-strings for this purpose, for reducing my previous template code from something like this:

Now I can do this, directly replacing variables:

However, sometimes it makes sense to have the template defined elsewhere — higher up in the code, or imported from a file or something. This means the template is a static string with formatting tags in it. Something would have to happen to the string to tell the interpreter to interpret the string as a new f-string, but I don't know if there is such a thing.

Is there any way to bring in a string and have it interpreted as an f-string to avoid using the .format(**locals()) call?

Ideally I want to be able to code like this... (where magic_fstring_function is where the part I don't understand comes in):

...with this desired output (without reading the file twice):

...but the actual output I get is:

0 投票
1 回答
90205 浏览

python - 如何在 f 字符串中转义大括号?

我有一个字符串,我想要花括号,但也利用 f-strings 功能。是否有一些适用于此的语法?

这是它不起作用的两种方法。我想将文字文本{bar}作为字符串的一部分。

NameError: name 'bar' is not defined

SyntaxError: f-string expression part cannot include a backslash

期望的结果:

编辑:看起来这个问题与如何在字符串中打印文字花括号字符并在其上使用 .format 具有相同的答案?,但只有知道它str.format使用与 f 字符串相同的规则时才能知道。所以希望这个问题在将 f-string 搜索者与这个答案联系起来时有价值。

0 投票
5 回答
42964 浏览

python - 使用大括号抑制解包列表的 f 字符串语法

我一直在使用新的 f 字符串格式检查我的一些字符串格式选项。我经常需要解压列表和其他未知长度的迭代。目前我使用以下...

这虽然有点麻烦,但使用 3.6 之前的 .format 表示法完成了这项工作。考虑到运行时字符串连接,新的 f 字符串格式选项很有趣。这是我遇到问题的 {} 数量的复制。在我之前的示例中,我只是创建了必要的结构并在 .format() 部分中解压缩。

尝试这样做会产生一种有效的变体,但是:

1)两个大括号一起不解包......

2) 在内部 {} 对周围添加空格:

这可行,但会留下左大括号 {, }:

2b) 将变体连接成一个 f 字符串

这使得外观和语法更好,因为评估显然是从左到右的。然而,这仍然留下了封闭的大括号:

3)尝试自动拆包{a}

也许,我对整个过程想得太多了,希望能有某种形式的自动解包。这只是产生了将大括号替换为的列表表示形式[]

抑制上述变体 (2) 中的大括号需要什么,或者我必须继续使用现有.format()方法吗?我想保持简单并使用 f-string 提供的新功能,而不是恢复到超出我目前熟悉的 Python 版本之前的版本。我开始怀疑 f'strings' 并没有完全覆盖其.format()兄弟提供的内容。我暂时将其保留,因为我什至没有冒险使用转义编码以及无法在 f 字符串中使用 \。我已经阅读了 PEP 并进行了广泛的搜索,但是,我觉得我错过了显而易见的东西,或者我想要的东西目前是不可能的。

几个小时后编辑:

4)使用下标手动切掉括号:str(a)[1:-2]

我确实找到了这个变体,它适用于我需要的某些情况

但是切片只是一种方便,并且仍然在结果周围留下字符串引号。

5) 以及来自@SenhorLucas 的最终解决方案

用尾随逗号解包。

0 投票
1 回答
1119 浏览

python - 为什么不能在 f-strings 中使用“await”?

为什么不能在 f-strings 中使用“await”?有没有办法强制 f 字符串在协程函数的上下文中评估格式表达式?

0 投票
5 回答
38249 浏览

python - f-strings 与 str.format()

.format()在我的 Python 3.5 项目中使用了很多,但我担心它会在下一个 Python 版本中被弃用,因为 f-strings 是一种新的字符串文字。

格式化字符串功能会完全取代旧的.format()吗?而从现在开始,是不是所有情况下都使用新样式会更好呢?

我知道这是基于“简单胜于复杂”的理念。但是,性能问题呢?它们之间有什么区别吗?或者它只是相同功能的简单外观?

0 投票
2 回答
80 浏览

python - 帮我用 f-string 修复这个代码?

当我读到这个

有这个代码

当我修改为 pep 498

它打印这个

0 投票
1 回答
107 浏览

python - 我如何使用 f-string 格式来重写语句:

我已经阅读并重新阅读了一些关于 PEP 和文字string插值的页面。
但是仍然无法通过实验找出确切的语法将起作用,因此我可以从python脚本中的以下语句中删除 %s 。

谢谢~

0 投票
7 回答
63519 浏览

python - 如何在 python 3.6 中使用 f-string 进行字典格式?

如何使用 python 3.6 F-String 执行这种格式?

0 投票
0 回答
845 浏览

python-3.x - Python 3.6.1 中的 F-strings:如果 f-strings 不使用局部或全局变量,那么 f-strings 如何发挥作用?我很困惑

我在 Python 3.6.1 文档中读到 f-strings 不使用本地或全局变量。虽然我确实理解 f-strings 不需要访问局部或全局变量中提供的多余信息的原因,但我不明白 f-string 在读取局部或全局变量时是如何工作的。

这是文档提供的示例:

产生以下输出:

代码中不是“name”、“age”和“anniversary”全局变量吗?如果是,f-string 没有使用这些全局变量吗?

这是我从中获取示例的文档的链接。

Python 3.6.1 文档:f-strings

感谢您的耐心和善意的帮助。