-3

Why did my_dict['key3'][0].upper() worked whereas my_dict['key3'][0].reverse() didnt work showing error as:

AttributeError                            Traceback (most recent call last)
<ipython-input-8-1cf579a91af7> in <module>
----> 1 my_dict['key3'][0].reverse()

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

i have assigned my_dict as:

my_dict={'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
Rjt
  • 15
  • 1
  • 1
    The error says exactly why: `AttributeError: 'str' object has no attribute 'reverse'`. There is no such method as `reverse` for `str` objects – DeepSpace Sep 24 '20 at 13:03
  • I highly suggest you try to execute the methods in a simpler environmente where you can make sure the methods you're calling produce the desired behaviour. If you attempted to call reverse in a single string, you'd see that the method does not exist (exacty what the error says). Also, please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – 89f3a1c Sep 24 '20 at 13:06
  • Only mutable sequences have the ```reverse``` method. Strings (```str```) are immutable. – Timus Sep 24 '20 at 13:09

3 Answers3

0

Simply because str has no reverse attribute as stated in the error. If you want to reverse a str object in Python, use this:

my_string [::-1]

Please read the error and search it on your web browser before coming here.

Jao
  • 558
  • 2
  • 12
  • but that is for example i have list = ['a','b','c'] and i do list.reverse() it will make the list as ['c','b','a'] – Rjt Sep 24 '20 at 13:12
  • They are different types `list` and `str`, you might be in need of a Python/CS/Programming course if you struggle with such concepts. – Jao Sep 24 '20 at 13:13
0

As the error says, there's no such method called reverse for str in Python.

You can simply do:

my_dict['key3'][0][::-1]
Yuan JI
  • 2,927
  • 2
  • 20
  • 29
  • but that is for example i have list = ['a','b','c'] and i do list.reverse() it will make the list as ['c','b','a'] – Rjt Sep 24 '20 at 13:10
  • 1
    because the `list` object has a method called `reverse`, but no such thing for `str` object – Yuan JI Sep 24 '20 at 13:12
0

Strings do not have an attribute called .reverse(). A useful trick in python to be able to inspect what attributes/methods are available to you is the dir function:

>>> dir('my string')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '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', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

You will notice no such reverse attribute. You can accomplish what you're after with the built-in reversed function or with slicing:

>>> ''.join(reversed('my string'))
'gnirts ym'

>>> 'my string'[::-1]
'gnirts ym'
TayTay
  • 6,882
  • 4
  • 44
  • 65