AttributeError: ‘str’ 对象的属性 ‘format’ 是只读的

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

AttributeError: 'str' object attribute 'format' is read-only

问题

我正在尝试练习格式化方法,如下所示:

Drinks = '{0},{1},{2}'
s = Drinks.format('coke', 'pepsi', 'miranda')
print(s)

然而,我遇到了这个错误:

> s = Drinks.format('coke', 'pepsi', 'miranda')
> AttributeError: 'str' object attribute 'format' is read-only
英文:

I'm trying to practice the format method as following:

Drinks = '{0},{1},{2}'
s = Drinks.format = ('coke', 'pepsi', 'miranda')
print(s)

However I get this error:

> s = Drinks.format = ('coke', 'pepsi', 'miranda') AttributeError: 'str'
> object attribute 'format' is read-only

答案1

得分: 1

Drink.format 是一个函数,你不需要为它赋值,只需调用它(用括号而不是等号):

s = Drink.format('coke', 'pepsi', 'miranda')
英文:

Drink.format is a function, you don't assign values to it, you simply call it (with parenthesis rather than an equal sign):

s = Drinks.format('coke', 'pepsi', 'miranda')

huangapple
  • 本文由 发表于 2023年7月23日 16:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747376.html
匿名

发表评论

匿名网友

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

确定