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 Overflow Top answer 1 of 4
7
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'
2 of 4
6
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
GeeksforGeeks
geeksforgeeks.org › python › reverse-string-python-5-different-ways
How to reverse a String in Python - GeeksforGeeks
Python provides a built-in function called reversed() which can be used to reverse the characters in a string. ... If we need more control over the reversal process then we can use a for loop to reverse the string.
Published March 3, 2026
7 proven methods to reverse the python string in 2021
"".join(sorted(a, reverse=True)) will not reverse a string. >>> a = "hello world" >>> "".join(sorted(a, reverse=True)) 'wroolllhed ' There's a deeper problem with articles like this, though. Reversing a string is a trivial task (i.e., it's something for a beginner to learn). Giving seven different methods with no explanation on if one is better than another is not good teaching, especially when some don't even work and others are pointlessly verbose. More on reddit.com
iterate over a string in reverse
https://docs.python.org/3/library/functions.html#reversed More on reddit.com
Why does [::1] reverse a string in Python?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
Videos
02:41
Python Tutorial : Reverse a String Using For Loop - YouTube
00:44
#Python Reverse a String Using For Loop. - YouTube
04:20
Python Interview Prep | Alternate Ways to Reverse a String using ...
03:47
How to Reverse a String in Python | Three Ways to Reverse a String ...
11:29
Python Program to Reverse a String using for loop - Hindi #18 - ...
DigitalOcean
digitalocean.com › community › tutorials › python-reverse-string
Python Reverse String - 5 Ways and the Best One | DigitalOcean
August 3, 2022 - Python String doesn’t have a built-in reverse() function. However, there are various ways to reverse a string in Python. ... Using Slicing to create a reverse copy of the string. Using for loop and appending characters in reverse order
STechies
stechies.com › 5-different-ways-reverse-string-python
Reverse String in Python Using 5 Different Methods
In python string library, there is no in-build “reverse” function to reverse a string, but there are many different ways to reverse a string. In this article, you will learn 5 different ways to reverse the string in Python. ... # Program to explain reverse string or sentence # Using for loop # Reverse String without using reverse function # Define a function def reverse_for(string): # Declare a string variable rstring = '' # Iterate string with for loop for x in string: # Appending chars in reverse order rstring = x + rstring return rstring string = 'Stechies' # Print Original and Reverse string print('Original String: ', string) print('Reverse String: ', reverse_for(string))
Python Examples
pythonexamples.org › python-reverse-string
Python - Reverse String
# Given string x = "Hello World" # Reverse string using For loop x_reversed = '' for c in x: x_reversed = c + x_reversed # Print reversed string print(x_reversed) ... In this example, we use a Python While Loop statement, and during each iteration, we decrement the length.
Linode
linode.com › docs › guides › how-to-reverse-a-string-in-python
How to Reverse a String in Python | Linode Docs
May 13, 2022 - if reversal_style == "loop": string_out_list = [] for item in string_in: string_out_list.insert(0, item) string_out = "".join(string_out_list) # Use the reversed function if indicated by reversal_style. elif reversal_style == "reversed": reversed_iterator = reversed(string_in) string_out = "".join(list(reversed_iterator)) # Reverse the order of words, rather than characters, in # the string if indicated by reversal_style. elif reversal_style == "word": word_list = string_in.split(" ") word_list = word_list[::-1] string_out = " ".join(word_list) # By default, use the slice approach to reverse t
Python Guides
pythonguides.com › python-program-to-reverse-a-string
How To Reverse A String In Python?
January 12, 2026 - Using the reversed() function is a clear and flexible way to reverse a string without slicing, especially when you want to understand how iteration works in Python. If you prefer a more manual approach, you can use a loop to reverse a string.
YouTube
youtube.com › watch
Three Ways to Reverse a String in Python TUTORIAL (using For Loops, reversed(), and Slice Notation) - YouTube
Python tutorial on 3 ways to reverse a string.This is a common python interview question. 📖 You can check out the Udemy course (Python Built-in Functions) h...
Published August 5, 2019
Medium
medium.com › @khasnobis.sanjit890 › python-reverse-string-74cc521cf8ca
Python Reverse String. Today we are going to write some code… | by Sanjit Khasnobis | Medium
September 10, 2023 - def reverseStr_slicemethod(inputStr): outputStr = inputStr[::-1] return outputStr def reverseStr_loopinglist_appendmethod(inputStr): outputStrlist = [] inputStrlist = list(inputStr) len_of_list = len(inputStrlist) for i in range(len_of_list): outputStrlist.append(inputStrlist[(len_of_list-i)-1]) outputStr = "".join(outputStrlist) return outputStr def reverseStr_reversed_method(inputStr): inputStrlist = list(inputStr) outputStrlist = reversed(inputStrlist) outputStr = "".join(outputStrlist) return outputStr def reverseStr_strconcat_method(inputStr): outputStr = '' for i in inputStr: outputStr =
Scaler
scaler.com › home › topics › string reverse in python
Reverse String in Python - Scaler Topics
April 8, 2022 - After iterating in reverse order, ... to reverse the string. We will recursively call the reverse() function and keep adding the last index character to the starting of the string....