在 Python 格式(f-string)字符串中,! r 是什么意思?

我知道 python3.6中新的 f 字符串是做什么的,但是结尾怎么样呢!在下面的代码片段中可以找到 r。

def __repr__(self):
return (f'Pizza({self.radius!r}, 'f'{self.ingredients!r})')
33491 次浏览

It just calls the repr of the value supplied.

It's usage is generally not really needed with f-strings since with them you can just do repr(self.radius) which is arguably more clear in its intent.

!r (repr), !s (str) and !a (ascii) were kept around just to ease compatibility with the str.format alternative, you don't need to use them with f-strings.