Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: '{0.lower()}'.format('A') yields 'str' object has no attribute 'lower()'

In Python strings have a method lower():

>>> dir('A')
[... 'ljust', 'lower', 'lstrip', ...]

However, when one tries '{0.lower()}'.format('A'), the response states:

>>> '{0.lower()}'.format('A')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lower()'

Can someone help me understand why the line above throws an AttributeError in this case? This seems like it should not be an AttributeError, though I must be mistaken. Any help understanding this would be very welcome!

Edit: I understand I can't call the lower() method inside the format call (though it'd be neat if that were possible); my question is why doing so throws an AttributeError. This error seems misleading in this case.

like image 707
duhaime Avatar asked Mar 31 '19 15:03

duhaime


2 Answers

You can't call a method from within a format specification. Dot notation inside the format specifier is a way to look up attribute names and render their values, not to call functions.

0.lower() tries to look up an attribute on the string literally named "lower()" - the equivalent of getattr(some_string, 'lower()'). You need to call the method before formatting.

>>> '{0.lower()}'.format('A')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lower()'
>>> '{0}'.format('A'.lower())
'a'
like image 71
snakecharmerb Avatar answered Sep 28 '22 17:09

snakecharmerb


As others have said, you can't do this in a format expression. It would work in an f-string though:

a = "A"
print(f"{a.lower()}")
like image 33
Daniel Roseman Avatar answered Sep 28 '22 15:09

Daniel Roseman