I am trying to run a python code but it's showing attribute error-

my_string = "Hello, world!"
my_string.reverse()

Output for the above code:

Traceback (most recent call last):
    File "c:\Users\name\OneDrive\Desktop\demo.py", line 2, in <module>
        my_string.reverse()
        ^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'reverse'

Could you please help me solve this?

Thanks in advance.

As the message says, strings do not have a "reverse" attribute (method or property). You can reverse a string by

newstring = my_string[::-1]

If you run idle or idlex and start a shell (interactive) window you can see your options by assigning a string to "my_string", then typing "my_string" followed by a dot, and then waiting for the intellisense pop up.

commented: !eciN +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.