AttributeError: 'str' object has no attribute 'uppercase'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'str' object has no attribute 'uppercase' #

The Python "AttributeError: 'str' object has no attribute 'uppercase'" occurs when we try to call the uppercase() method on a string. To solve the error, use the upper() method to uppercase a string, e.g. 'abc'.upper().

attributeerror str object has no attribute uppercase

Here is an example of how the error occurs.

main.py
my_str = 'hello world' # ⛔️ AttributeError: 'str' object has no attribute 'uppercase' result = my_str.uppercase()

Strings don't have an uppercase() method in Python, however you can use the upper() method to uppercase a string.

main.py
my_str = 'hello world' result = my_str.upper() print(result) # 👉️ "HELLO WORLD"

The str.upper method returns a copy of the string with all the cased characters converted to uppercase.

The method does not change the original string, it returns a new string. Strings are immutable in Python.

Make sure to store the result in a variable as the original string remains unchanged.

Conversely, you can use the lower() method to convert a string to lowercase.

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

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

You can use the str.title() method to get the titlecased version of a string.

main.py
my_str = 'james doe' result = my_str.title() print(result) # 👉️ "James Doe"

The str.title method returns a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.

Note that the algorithm also converts characters after apostrophes to uppercase.

main.py
my_str = "it's him" result = my_str.title() print(result) # 👉️ "It'S Him"

The string.capwords method doesn't have this problem, as it only splits words on spaces.

main.py
from string import capwords my_str = "it's him" result = capwords(my_str) print(result) # 👉️ "It's Him"

A good way to start debugging is to print(dir(your_object)) and see what attributes a string has.

Here is an example of what printing the attributes of a string looks like.

main.py
my_string = 'hello world' # [ 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', # 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', # 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', # 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', # 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', # 'title', 'translate', 'upper', 'zfill'] print(dir(my_string))

If you pass a class to the dir() function, it returns a list of names of the classes' attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: str object has no attribute error".

Since the str object doesn't implement a uppercase() method, the error is caused.

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.