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

标签 python python-3.6 f-string

<分区>

我明白了 python 3.6 中新的 f 字符串的作用,但结尾 !r 又如何呢,如下面的代码片段所示。

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

最佳答案

它只是调用所提供值的 repr

对于 f-strings 通常不需要它的用法,因为使用它们你可以只做 repr(self.radius) ,这在其意图上可以说更清楚。

!r (repr), !s (str) 和 !a (ascii) 被保留只是为了简化与 str.format 替代方案的兼容性,您不需要将它们与 f-strings 一起使用。

关于python - 在 Python 格式(f 字符串)字符串中,!r 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44800801/

相关文章:

python - Python 3.6+ 中是否有格式化的字节字符串文字?

Python将文件夹中的所有JPGS转换为PDF

python - 使用 matplotlib 在 loglog 图中设置轴限制

python - 获取具有特定值的单元格的列和行标签

python - 我可以在解释器解析代码之前断言 python 版本吗?

python - 为什么 f-strings 在它们引用的变量发生变化时不发生变化?

python - 将 Python 版本 3.6 更改为 3.10 后,我无法从 'Callable' 导入名称 'collections'

python - theano中定义函数的正确方法?

python - python 中的多线程客户端

python - ModuleNotFoundError : What does it mean __main__ is not a package?