+ 1

How to properly reverse a string in python

This is the code text = "Sololearn" reversed_text = text.reverse() print(reversed_text) I expected "nraeloloS" but got an error instead. Can someone explain why and show the correct way?

15th Sep 2025, 6:10 AM
Alex Bright Chizaram
Alex Bright Chizaram - avatar
3 Respuestas
0
In Python, text[::-1] is a slicing operation applied to a sequence (like a string, list, or tuple) that effectively reverses the order of its elements. AI ✍️(◔◡◔)
15th Sep 2025, 6:34 AM
SoloProg
SoloProg - avatar
0
.reverse() is a list method in python, not for strings , they're immutable. you can reverse a string using slicing in python :. text = "Sololearn" reversed_text = text[::-1] print(reversed_text) #nraeloloS
15th Sep 2025, 6:42 AM
ℛℇℋⅅ
ℛℇℋⅅ - avatar
0
Ahh, I see now! 😅 I didn’t know strings had no .reverse() method. Thanks for explaining! Python really likes to keep us on our toes, huh? 🐍
15th Sep 2025, 6:51 AM
Alex Bright Chizaram
Alex Bright Chizaram - avatar