【发布时间】:2019-11-14 22:13:10
【问题描述】:
我在 python 中探索格式字符串时遇到了使用整数来识别位置参数的手动字段规范。
据我所知,格式字符串只支持两个运算符:. (getattr) 和 [] (getitem)
但是当我尝试使用它时,我得到了一个错误。
"The story of a {0} {1.upper()}".format("cunning", "fox")
这给了
AttributeError: 'str' object has no attribute 'upper()'
但是,当我这样做时:
"The story of a {0} {1.upper}".format("cunning", "fox")
它给出了输出:
The story of a cunning <built-in method upper of str object at 0x7f65756e1e18>
有人能解释一下为什么会这样吗?我在这里错过了什么?
【问题讨论】:
标签: python python-3.x format-string