+ 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?
3 Risposte
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 ✍️(◔◡◔)
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
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? 🐍