在Python中引用F-string中的字符串值。

huangapple go评论65阅读模式
英文:

Quote string value in F-string in Python

问题

f'这是我想引用的值:\'{value}\''
f'这是我想引用的值:{value!r}'

你可以选择使用单引号或双引号。

英文:

I'm trying to quote one of the values I send to an f-string in Python:

f'This is the value I want quoted: \'{value}\''

This works, but I wonder if there's a formatting option that does this for me, similar to how %q works in Go. Basically, I'm looking for something like this:

f'This is the value I want quoted: {value:q}'
>>> This is the value I want quoted: 'value'

I would also be okay with double-quotes. Is this possible?

答案1

得分: 5

使用显式转换标志 !r

>>> value = 'foo'
>>> f'This is the value I want quoted: {value!r}'
"This is the value I want quoted: 'foo'"

r代表reprf'{value!r}'的结果应等效于使用f'{repr(value)}'(这是在f-strings之前引入的功能)。

出于某种在PEP中未记录的原因,还有一个!a标志,它使用ascii进行转换:

>>> f'quote {"\u128293"!a}'
"quote '\U0001f525'"

还有一个!s标志用于str,似乎没什么用... 除非你知道对象可以覆盖其格式化程序以执行与object.__format__不同的操作。它提供了一种退出这些花哨操作并仍然使用__str__的方法。

>>> class What:
...     def __format__(self, spec):
...         if spec == "fancy":
...             return "🀅🀄🀉🀒🀉🀄"
...         return "potato"
...     def __str__(self):
...         return "spam"
...     def __repr__(self):
...         return "<wacky object at 0xcafef00d>"
...
>>> obj = What()
>>> f'{obj}'
'potato'
>>> f'{obj:fancy}'
'🀅🀄🀉🀒🀉🀄'
>>> f'{obj!s}'
'spam'
>>> f'{obj!r}'
'<wacky object at 0xcafef00d>'
英文:

Use the explicit conversion flag !r:

&gt;&gt;&gt; value = &#39;foo&#39;
&gt;&gt;&gt; f&#39;This is the value I want quoted: {value!r}&#39;
&quot;This is the value I want quoted: &#39;foo&#39;&quot;

The r stands for repr; the result of f&#39;{value!r}&#39; should be equivalent to using f&#39;{repr(value)}&#39; (it's a feature that predates f-strings).

For some reason undocumented in the PEP, there's also an !a flag which converts with ascii:

&gt;&gt;&gt; f&#39;quote {&quot;&#128293;&quot;!a}&#39;
&quot;quote &#39;\\U0001f525&#39;&quot;

And there's an !s for str, which seems useless... unless you know that objects can override their formatter to do something different than object.__format__ does. It provides a way to opt-out of those shenanigans and use __str__ anyway.

&gt;&gt;&gt; class What:
...     def __format__(self, spec):
...         if spec == &quot;fancy&quot;:
...             return &quot;&#120005;&#119900;&#120009;&#119990;&#120009;&#119900;&quot;
...         return &quot;potato&quot;
...     def __str__(self):
...         return &quot;spam&quot;
...     def __repr__(self):
...         return &quot;&lt;wacky object at 0xcafef00d&gt;&quot;
... 
&gt;&gt;&gt; obj = What()
&gt;&gt;&gt; f&#39;{obj}&#39;
&#39;potato&#39;
&gt;&gt;&gt; f&#39;{obj:fancy}&#39;
&#39;&#120005;&#119900;&#120009;&#119990;&#120009;&#119900;&#39;
&gt;&gt;&gt; f&#39;{obj!s}&#39;
&#39;spam&#39;
&gt;&gt;&gt; f&#39;{obj!r}&#39;
&#39;&lt;wacky object at 0xcafef00d&gt;&#39;

答案2

得分: 0

另一种方法也可以只使用字符串格式化,例如:

string = "Hello, this is a '%s', '%d' is a decimal, '%f' is a float" % ("string", 3, 5.5)
print(string)

这将返回:

Hello, this is a 'string', '3' is a decimal, '5.500000' is a float
英文:

Also another way could be just string formatting as for example:

string =&quot;Hello, this is a &#39;%s&#39;, &#39;%d&#39; is a decimal, &#39;%f&#39; is a float&quot;%(&quot;string&quot;, 3, 5.5)
print(string)

This would return:

Hello, this is a &#39;string&#39;, &#39;3&#39; is a decimal, &#39;5.500000&#39; is a float

huangapple
  • 本文由 发表于 2023年6月22日 10:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定