Python string is not mutable, so you can not use the del statement to remove characters in place. However you can build up a new string while looping through the original one:
def reverse(text):
rev_text = ""
for char in text:
rev_text = char + rev_text
return rev_text
reverse("hello")
# 'olleh'
Answer from akuiper on Stack OverflowPython string is not mutable, so you can not use the del statement to remove characters in place. However you can build up a new string while looping through the original one:
def reverse(text):
rev_text = ""
for char in text:
rev_text = char + rev_text
return rev_text
reverse("hello")
# 'olleh'
The problem is that you can't use del on a string in python.
However this code works without del and will hopefully do the trick:
def reverse(text):
a = ""
for i in range(1, len(text) + 1):
a += text[len(text) - i]
return a
print(reverse("Hello World!")) # prints: !dlroW olleH
Videos
How to reverse a string in Python without a reverse function?
Can you use reverse () on a string?
What is a reversed function in Python?
I want to iterate over a string in reverse. I googled it and found this way
for i in range(len(k)-1, 0-1, -1):
but to be honest I don't understand it. can you please explain it to me?
also, I would love it if you guys can suggest other methods
How to reverse the python string now in 2021?
Hello to all python buddies,
You're stirring your cofee, and going to read r/Python. And you love the blog post.
Today, I'm going to make r/Python more lovable to you.
I'm going to show you the 6 proven methods to reverse the python string. Which are easy and quick to do.
So, start these methods
โบ๏ธ
Reverse the string using slice method
You can reverse the string using slice method.
The slice indicates the [start:end] position.
A start is a position where sequence start. and end is the position where sequence ends.
The first position is 0th index.
So, here you can use [::-1].
The [::-1] means sequence starting from last of the string.
For example,
a = ["hello"]
print(a[::-1])
It'll reverse the python string.
>>> olleh
2. Reversed the string using reversed() &join() methods
First of all, the reversed() method reverse the sequence.
After reversed() with you can join() every iterables as string.
Basically, the join() method join the iterables as a string seperator.
reversed() & join()After running, this code you'll get something like
๐
output3. Reversed the string: join() and sorted() method
As you know, sorted() sort the string or sequences in ascending or descending method.
Here, I'm going to use descending order.
For descending order, pass reverse = True inside sorted().
And previously, I've told that join joins the sequences as a string seperator.
For example,
join() & sorted()Here, you can see that first I've sorted the string in descending order.
After that, I've join every character as a string.
When you run above code, you'll get:--->
outputSo, you've get the reversed string as output.
4. Reversed the string using for loop
You can reverse the string using for loop.
To create the reverse string in for loop, you need function with empty string.
The every new string add to the empty string.
After adding, all the string it becomes the reverse string.
For example,
codeAfter running code, you'll get--->
outputSo, here you've seen how to reverse the python string. I've told you the 6 methods.
And here I've shown you the 4 methods.
But I'm going to show you 3 methods more.
That means 7 method for reverse the python string.
So, I've given you 1 bonus method.
To get these 3 methods, check out the
๐
https://www.heypython.com/python-programming/reverse-the-python-string/
Try the reversed builtin:
for c in reversed(string):
print c
The reversed() call will make an iterator rather than copying the entire string.
PEP 322 details the motivation for reversed() and its advantages over other approaches.
EDIT: It has been quite some time since I wrote this answer. It is not a very pythonic or even efficient way to loop over a string backwards. It does show how one could utilize range and negative step values to build a value by looping through a string and adding elements in off the end of the string to the front of the new value. But this is error prone and the builtin function reversed is a much better approach. For those readers attempting to understand how reversed is implemented, take a look at the PEP, number 322, to get an understanding of the how and why. The function checks whether the argument is iterable and then yields the last element of a list until there are no more elements to yield. From the PEP:
[reversed] makes a reverse iterator over sequence objects that support getitem() and len().
So to reverse a string, consume the iterator until it is exhausted. Without using the builtin, it might look something like,
def reverse_string(x: str) -> str:
i = len(x)
while i > 0:
i -= 1
yield x[i]
Consume the iterator either by looping, eg
for element in (reverse_string('abc')):
print(element)
Or calling a constructor like:
cba = list(reverse_string('abc'))
The reverse_string code is almost identical to the PEP with a check removed for simplicity's sake. In practice, use the builtin.
ORIGNAL ANSWER:
Here is a way to reverse a string without utilizing the built in features such as reversed. Negative step values traverse backwards.
def reverse(text):
rev = ''
for i in range(len(text), 0, -1):
rev += text[i-1]
return rev