Select Page

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

by | Programming, Python, Tips

This error occurs when you try to reverse a string by calling reverse() directly on the string object. The reverse() method belongs to the List data type, not String. You can solve this error by using the subscript operator, for example,

reversed_str = a_str[::-1]

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


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

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 ‘reverse’” tells us that the string object does not have the attribute reverse().

The reverse() method belongs to the List data type and returns the iterator object with the elements in reverse order. We can convert a string to a List using the split() method or the sorted() method.

We can check an object’s attributes by using the dir() function. The dir() function returns all the properties and methods of the specified object as a list.

Let’s verify that reverse() is not a String method by using the in operator:

string = "test"

attributes = dir(string)

print("reverse" in attributes)
False

Let’s prove that reverse() is a List method by using the in operator:

lst = [1, 2, 3]
attributes = dir(lst)

print("reverse" in attributes)
True

Example

Let’s look at an example of a string we want in reverse. We will attempt to call the reverse() method on the string.

a_str = 'xyz'

reverse_str = a_str.reverse()

print(reverse_str)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 3>()
      1 a_str = 'xyz'
----> 3 reverse_str = a_str.reverse()
      5 print(reverse_str)

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

The error occurs because the reverse method belongs to the List data type, not the String data type.

Solution #1

We can solve this error by using string slicing to reverse a string.

a_str = 'xyz'

reverse_str = a_str[::-1]

print(reverse_str)

In the above code, we create a slice that starts from the end of the string and ends at position 0, moving with step -1 (or one step backward). Let’s run the code to see the result.

zyx

We successfully reversed the string using slicing.

Solution #2

We can also reverse a string by using the builtin reversed() function. The reversed function accepts a sequence and returns a reversed iterator object.

a_str = 'xyz'

reverse_str = ''.join(reversed(a_str))

print(reverse_str)
a_str = 'xyz'

reverse_str = ''.join(reversed(a_str))

print(reverse_str)

We can pass the iterator object to the join() method, which will return the reversed string. Let’s run the code to see the result.

zyx

We successfully reversed the string using the reversed() method.

Solution #3

We can also solve the error by using a while loop. First, we need to define a new list to store the characters from the string.

a_str = "xyz"

reversedString = [] 

index = len(a_str)

Next, we will loop over the characters in the string with an iterating variable called index initialized with the length of the string.

Each iteration will append a character from the string to the list in reverse order and decrement the index by 1.

Once the while loop is complete, we will use the join() method to join the individual characters in the list to a string.

while index > 0:
    reversedString += a_str[index-1]
    index-=1

print(''.join(reversedString))

Let’s run the code to see the result:

zyx

We successfully reversed the string using a loop.

Summary

Congratulations on reading to the end of this tutorial! The AttribueError: ‘str’ object has no attribute ‘reverse’ occurs when you call the reverse() method on a string object. Ensure that you check the object type before calling the reverse() method using the type() function.

For further reading on AttributeErrors, go to the article:

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

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!