AttributeError: ‘str’ object has no attribute in Python – How To Fix It?

AttributeError: ‘str’ object has no attribute in Python

In this article, we will show the cause of the error “AttributeError: ‘str’ object has no attribute” in Python and helps you solve it optimally. Read on to learn more.

When does the error “AttributeError: ‘str’ object has no attribute” in Python occur?

The “AttributeError: ‘str’ object has no attribute” in Python is thrown when you try to access a property on an object that does not have that attribute.

For example, the error line “AttributeError: ‘str’ object has no attribute ‘append’” tells you that there is no attribute named ‘append’ in the ‘str’ object.

myStr = "learshareit"
print(myStr.append(".com"))

Output:

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

To fix errors like the above code sample, we will guide you through three ways to handle them when you encounter them.

How to fix this error?

Using try … except

You can read more about the syntax of try ... except here.

In this case, we use try ... except to add a character to the str variable. If we can’t, we’ll get a message so we can recheck the attribute. See the sample code below for a better understanding.

myStr = "learshareit"

try:
	print(myStr.append(".com"))
except AttributeError:
	print("Please check the attribute in string!!!")

Output:

Please check the attribute in string!!!

Using hasattr() function

The hasattr() function in Python is used to check if the passed object has the property you want to find.

Syntax:

hasattr(object, attribute)

Parameters:

  • object: the object you want to test.
  • attribute: the name of the attribute you want to check.

We will pass the first parameter as the object we want to check and the second parameter as the name of the attribute we want to find. Then the result will return True or False. If true, we can use that attribute; if false, we cannot use that attribute. Like this:

string = 'learnshareit'

if hasattr(string, 'append'):
	print(string.append())
else:
	print('Cannot use this attribute in string!!!')

Output:

Cannot use this attribute in string!!!

Using the dir() function and try … except

The dir() function returns a list of the valid properties of the object.

Syntax:

dir(object)

Parameters:

  • object: an object that you want to get all the attributes. 

First, we’ll use the dir() function to get a list of the string attributes. Then we use the index() function to check if the property name we are trying to use is in the property list. If it exists, the code in the try block will execute. If not found, the code in the except block will execute. We will see a message line instead of stopping the program because of the AttributeError. See the code sample below for a better understanding.

myStr = 'learnshareit'

# Generate a list of the attributes contained in string
attributes = dir(myStr)

try:
	num = attributes.index('append')
	print(myStr.append('.com'))
except ValueError:
	print('Please check the attribute in string!!!')

Output:

Please check the attribute in string!!!

Summary

We have just shown you four ways to avoid and fix the error “AttributeError: ‘str’ object has no attribute” in Python while working with ‘str’ objects. We hope this article helps you to understand the problem better.

Have a lucky day!

Maybe you are interested in similar errors:

Leave a Reply

Your email address will not be published. Required fields are marked *