How to Solve Python AttributeError: ‘str’ object has no attribute ‘uppercase’

by | Programming, Python, Tips

This error occurs when you try to call uppercase() on a string to convert the characters to uppercase. You can solve the error by calling the string method upper() to convert the string to uppercase. For example,

my_str = 'python is fun'

my_str_upper = my_str.upper()

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘str’ object has no attribute ‘uppercase’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘str’ object has no attribute ‘uppercase’” tells us that the string object we handle does not have the attribute uppercase().

We can check if an attribute exists for an object using the dir() function. For example,

my_str = 'Python'

print(type(my_str))

print('uppercase' in dir(my_str))
<class 'str'>
False

We can see that uppercase() is not in the list of attributes for the str object.

Example

Let’s look at an example of trying to call the uppercase() method on a string.

# Define string

my_str = 'research'

# Try to convert string to uppercase

result = my_str.uppercase()

print(result)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 7>()
      3 my_str = 'research'
      5 # Try to convert string to uppercase
----> 7 result = my_str.uppercase()
      9 print(result)

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

The error occurs because uppercase() is not a string method in Python.

Solution

We can solve the error by calling the str.upper() method that returns a copy of the string where all characters are in uppercase. Let’s look at the revised code:

# Define string

my_str = 'research'

# Try to convert string to uppercase

result = my_str.upper()

print(result)

Let’s run the code to get the result:

RESEARCH

Summary

Congratulations on reading to the end of this tutorial!

For further reading on AttributeErrors with string objects, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨