AttributeError: 'int' object has no attribute 'lower'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'int' object has no attribute 'lower' #

The Python "AttributeError: 'int' object has no attribute 'lower'" occurs when we call the lower() method on an integer. To solve the error, make sure the value you are calling lower on is of type string.

attributeerror int object has no attribute lower

Here is an example of how the error occurs.

main.py
example = 'HELLO WORLD' example = 100 print(type(example)) # 👉️ <class 'int'> # ⛔️ AttributeError: 'int' object has no attribute 'lower' print(example.lower())

We reassigned the example variable to an integer and tried to call the lower() method on the integer which caused the error.

If you print() the value you are calling lower() on, it will be an integer.

To solve the error, you need to track down where exactly you are setting the value to an integer in your code and correct the assignment.

If the value is expected to be an integer, there is no point of calling lower() on it and you can remove the call to the method.

To solve the error in the example, we would have to remove the reassignment or correct it.

main.py
example = 'HELLO WORLD' print(example.lower()) # 👉️ "hello world"

The lower() method returns a copy of the string with all the cased characters converted to lowercase.

If you need to silence the error and can't remove the call to lower() you can convert the integer to a string before calling lower().

main.py
example = 100 print(str(example).lower()) # 👉️ "100"

However, calling the lower() method on a numeric string has no effect at all.

You might also be assigning the result of calling a function that returns an integer to a variable.

main.py
def get_string(): return 100 my_string = get_string() # ⛔️ AttributeError: 'int' object has no attribute 'lower' print(my_string.lower())

The my_string variable gets assigned to the result of calling the get_string function.

The function returns an integer, so we aren't able to call lower() on it.

To solve the error, you have to track down where the specific variable gets assigned an integer instead of a string and correct the assignment.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.