'aBcDe'.lower()
works fine and returns 'abcde'
.
So why can't I do this : '{0.lower()}'.format('aBcDe')
?
It returns the following error: AttributeError: 'str' object has no attribute 'lower()'
My guess is that the code figures out that the attribute is the entire string 'lower()'
, instead of 'lower'
and it just doesn't work. So how can I call a method in this fashion?
CodePudding user response:
You're right; Python thinks that .lower()
is an attribute of the string 'aBcDe'
. You can fix this by moving the call to .lower()
to the parameter to .format()
:
'{0}'.format('aBcDe'.lower())
CodePudding user response:
With the format
method, you can't put something you want to evaluate inside the curly braces. Here are two (of several ways around this):
'{0}'.format('aBcDe'.lower())
or use an f-string:
f"{'aBcDe'.lower()}"