英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论