簡體   English   中英

如何在一行中編寫帶有“加號和減號”符號的 Python f 字符串?

[英]How to write a Python f-string with the “plus and minus” symbol in a single line?

加號和減號 (±) 的 unicode 是u"\±" 我似乎無法在一行中使用 f-string 編寫提交此 unicode。 見下文。

>>> print( f'{u"\u00B1"}2 stddev' )
SyntaxError: f-string expression part cannot include a backslash

相反,我必須這樣做:

>>> plusminus = u"\u00B1"
>>> print( f'{plusminus}2 stddev' )
±2 stddev

我想用一行來寫。 我如何用 f-string 做到這一點?

對於非 f 字符串,我可以這樣做:

>>> print( '\u00B12 stddev' )
±2 stddev

這會做
print( f'\±2 stddev' )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM