+ 5

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
Alex Bright - avatar
34 Antwoorden
+ 9
Alex Bright Chizaram , besides of all the mentioned solutions, we can also use the reversed() built-in function: text = 'hello world' print(''.join(reversed(text)))
15th Sep 2025, 12:49 PM
Lothar
Lothar - avatar
+ 5
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
+ 4
A natural number is given. Write a program that reverses the order of the digits in the number. number = int(input()) inverted = 0 while number !=0: inverted = number % 10 print(inverted, end='') number = number // 10
15th Sep 2025, 8:15 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 4
About recursion ... I haven't formed an opinion on the use of recursion yet. Because on the one hand, if you use it for operations on numbers. And there are no restrictions on the length of numbers in python. You are limited only by the amount of free computer memory. On the other hand, because a recursive function in the code calls itself. This means that it is sent to the stack until it is fully executed. Here, you may probably encounter a buffer overflow error if you enter a very large number as input.
15th Sep 2025, 8:36 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
Hi! Please, see this tutorial: 👇👍💪🔥😁 https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK/reverse-string-python-5-different-ways/ __________________________________ def reversed1(variable): res='' for i in range(len(variable)-1,-1,-1): res+=variable[i] return res n = reversed1(input()) print(n) ___________________________________ def reversed1(variable): res=[] for i in range(len(variable)-1,-1,-1): res.append(variable[i]) res=''.join(res) return res n = reversed1(input()) print(n) ____________________________________ def reversed3(variable): if len(variable) == 1: return variable return variable[-1] + reversed3(variable[:-1]) n = reversed3(input()) print(n) ____________________________________ def reversed4(variable): res=''.join(reversed(variable)) return res n = reversed4(input()) print(n)
15th Sep 2025, 7:50 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
The program displays the entered string of numbers in reverse order: number = int(input()) separate = 0 while number !=0: separate = number %10 print (separate) number = number // 10
15th Sep 2025, 8:10 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
Wow, that’s a really clear explanation! 😄 I get what you mean about the stack—recursion is super elegant, but it can definitely hit limits if the input is huge. I usually stick to loops for reversing numbers or strings in real projects, especially when I don’t know how big the input might be.
15th Sep 2025, 10:17 AM
Alex Bright
Alex Bright - avatar
+ 3
Ah yes! 😄 I love how Python gives us so many ways to do the same thing. Using reversed() is super clean and readable, and I like that it works for strings and lists alike.
15th Sep 2025, 12:58 PM
Alex Bright
Alex Bright - avatar
+ 3
seeing all these methods side by side really shows how flexible Python can be! Which one do you usually use when you just want to reverse a string quickly?
15th Sep 2025, 1:15 PM
Alex Bright
Alex Bright - avatar
+ 2
.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
+ 2
Which method do you usually use in real projects? I’m curious what Python pros prefer! 🐍
15th Sep 2025, 7:59 AM
Alex Bright
Alex Bright - avatar
+ 2
The recursion one feels like magic ✨—I’ll need a few more coffees to fully wrap my head around it ☕ Do you think using reversed() is cheating or smart? 😎
15th Sep 2025, 8:01 AM
Alex Bright
Alex Bright - avatar
+ 2
Haha, I love that! 😄 Honestly, I’m still learning too, so I totally get where you’re coming from. For small scripts or quick tasks, I usually go with text[::-1]—super short and Pythonic 🐍 But seeing all these loop and recursion methods makes me realize there’s more than one way to do things.
15th Sep 2025, 8:11 AM
Alex Bright
Alex Bright - avatar
+ 2
Haha, that’s awesome! 😄 Loops are a great way to understand how numbers and digits work in Python. I remember doing something similar when I began learning too.
15th Sep 2025, 8:14 AM
Alex Bright
Alex Bright - avatar
+ 2
But experimenting with loops like this is perfect for building your problem-solving skills, so you’re definitely on the right track!
15th Sep 2025, 8:15 AM
Alex Bright
Alex Bright - avatar
+ 2
We are on the right track Every minute of your time really worth it we learn everyday
15th Sep 2025, 8:20 AM
Alex Bright
Alex Bright - avatar
+ 2
You can use this one: text = input() reverxed = text[::-1] print(reverxed) Or a one-liner: print(input()[::-1])
16th Sep 2025, 8:33 AM
Jeremiah Haziel Guadarrama
Jeremiah Haziel Guadarrama - avatar
+ 2
text = "Sololearn" reversed_text = text[::-1] print(reversed_text)
16th Sep 2025, 8:54 AM
Seyyed Mohammad Taghi Aghaamou
Seyyed Mohammad Taghi Aghaamou - avatar
+ 2
Alex Bright The professional method is [::-1] All these recursion etc aren't needed for reversing string, you are just adding more to the execution time and that makes your boss suspicious of your coding skills. Same goes for "".join(reversed(... Just use the slicing method [::-1]
17th Sep 2025, 5:50 AM
RuntimeTerror
RuntimeTerror - avatar
+ 1
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
Alex Bright - avatar