Search code examples
pythonstring-formattingattributeerror

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.


Solution

  • 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'