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 OverflowI 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
Best way to loop over a python string backwards - Stack Overflow
python - Trying to reverse a string using a while loop & other specific conditions - Stack Overflow
iterate over a string in reverse
7 proven methods to reverse the python string in 2021
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?
Videos
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'
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
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
If we were to correct the solution you have provided, it would be this:
name= str((input("Enter the name: ")))
i = len(name) - 1
while i >= 0:
print(name[i], end = '')
i = i - 1
A better way to do it in the manner you are attempting is to use a for loop and count backwards through the string indices.
for i in range(1, len(name) + 1):
print(name[-i])
This will iterate through a range of numbers (from 1 to n) and count backaward along your string by calling the negative indices of the string. 'helloworld'[-1] = d ...[-2] = l ...[-3] = r etc.