+ 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?
34 Antworten
+ 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)))
+ 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 âïž(ââĄâ)
+ 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
+ 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.
+ 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)
+ 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
+ 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.
+ 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.
+ 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?
+ 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
+ 2
Which method do you usually use in real projects? Iâm curious what Python pros prefer! đ
+ 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? đ
+ 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.
+ 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.
+ 2
But experimenting with loops like this is perfect for building your problem-solving skills, so youâre definitely on the right track!
+ 2
We are on the right track
Every minute of your time really worth it we learn everyday
+ 2
You can use this one:
text = input()
reverxed = text[::-1]
print(reverxed)
Or a one-liner:
print(input()[::-1])
+ 2
text = "Sololearn"
reversed_text = text[::-1]
print(reversed_text)
+ 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]
+ 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? đ