Using slicing:

>>> 'hello world'[::-1]
'dlrow olleh'

Slice notation takes the form [start:stop:step]. In this case, we omit the start and stop positions since we want the whole string. We also use step = -1, which means, "repeatedly step from right to left by 1 character".

Answer from Paolo Bergantino on Stack Overflow
🌐
w3resource
w3resource.com › python-exercises › python-functions-exercise-4.php
Python Exercise: Reverse a string - w3resource
July 12, 2025 - # Define a function named 'string_reverse' that takes a string 'str1' as input def string_reverse(str1): # Initialize an empty string 'rstr1' to store the reversed string rstr1 = '' # Calculate the length of the input string 'str1' index = len(str1) ...
🌐
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_reversed_method(inputStr): inputStrlist = list(inputStr) outputStrlist = reversed(inputStrlist) outputStr = "".join(outputStrlist) return outputStr ... Here, we have used python inbuilt framework reversed method.
Top answer
1 of 14
3168

Using slicing:

>>> 'hello world'[::-1]
'dlrow olleh'

Slice notation takes the form [start:stop:step]. In this case, we omit the start and stop positions since we want the whole string. We also use step = -1, which means, "repeatedly step from right to left by 1 character".

2 of 14
329

What is the best way of implementing a reverse function for strings?

My own experience with this question is academic. However, if you're a pro looking for the quick answer, use a slice that steps by -1:

>>> 'a string'[::-1]
'gnirts a'

or more readably (but slower due to the method name lookups and the fact that join forms a list when given an iterator), str.join:

>>> ''.join(reversed('a string'))
'gnirts a'

or for readability and reusability, put the slice in a function

def reversed_string(a_string):
    return a_string[::-1]

and then:

>>> reversed_string('a_string')
'gnirts_a'

Longer explanation

If you're interested in the academic exposition, please keep reading.

There is no built-in reverse function in Python's str object.

Here is a couple of things about Python's strings you should know:

  1. In Python, strings are immutable. Changing a string does not modify the string. It creates a new one.

  2. Strings are sliceable. Slicing a string gives you a new string from one point in the string, backwards or forwards, to another point, by given increments. They take slice notation or a slice object in a subscript:

    string[subscript]
    

The subscript creates a slice by including a colon within the braces:

    string[start:stop:step]

To create a slice outside of the braces, you'll need to create a slice object:

    slice_obj = slice(start, stop, step)
    string[slice_obj]

A readable approach:

While ''.join(reversed('foo')) is readable, it requires calling a string method, str.join, on another called function, which can be rather relatively slow. Let's put this in a function - we'll come back to it:

def reverse_string_readable_answer(string):
    return ''.join(reversed(string))

Most performant approach:

Much faster is using a reverse slice:

'foo'[::-1]

But how can we make this more readable and understandable to someone less familiar with slices or the intent of the original author? Let's create a slice object outside of the subscript notation, give it a descriptive name, and pass it to the subscript notation.

start = stop = None
step = -1
reverse_slice = slice(start, stop, step)
'foo'[reverse_slice]

Implement as Function

To actually implement this as a function, I think it is semantically clear enough to simply use a descriptive name:

def reversed_string(a_string):
    return a_string[::-1]

And usage is simply:

reversed_string('foo')

What your teacher probably wants:

If you have an instructor, they probably want you to start with an empty string, and build up a new string from the old one. You can do this with pure syntax and literals using a while loop:

def reverse_a_string_slowly(a_string):
    new_string = ''
    index = len(a_string)
    while index:
        index -= 1                    # index = index - 1
        new_string += a_string[index] # new_string = new_string + character
    return new_string

This is theoretically bad because, remember, strings are immutable - so every time where it looks like you're appending a character onto your new_string, it's theoretically creating a new string every time! However, CPython knows how to optimize this in certain cases, of which this trivial case is one.

Best Practice

Theoretically better is to collect your substrings in a list, and join them later:

def reverse_a_string_more_slowly(a_string):
    new_strings = []
    index = len(a_string)
    while index:
        index -= 1                       
        new_strings.append(a_string[index])
    return ''.join(new_strings)

However, as we will see in the timings below for CPython, this actually takes longer, because CPython can optimize the string concatenation.

Timings

Here are the timings:

>>> a_string = 'amanaplanacanalpanama' * 10
>>> min(timeit.repeat(lambda: reverse_string_readable_answer(a_string)))
10.38789987564087
>>> min(timeit.repeat(lambda: reversed_string(a_string)))
0.6622700691223145
>>> min(timeit.repeat(lambda: reverse_a_string_slowly(a_string)))
25.756799936294556
>>> min(timeit.repeat(lambda: reverse_a_string_more_slowly(a_string)))
38.73570013046265

CPython optimizes string concatenation, whereas other implementations may not:

... do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b . This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.

🌐
W3Schools
w3schools.com › python › python_howto_reverse_string.asp
How to reverse a String in Python
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... Learn how to reverse a String in Python.
🌐
Tutorial Gateway
tutorialgateway.org › python-program-to-reverse-string
Python Program to Reverse a String
January 4, 2026 - The following program allows the user to enter any sentence. Next, we declared an empty string to store the reversed result. The for loop will iterate the string characters from start to end. Within the loop, we add each character to the starting position and push the existing one to the next position. For example, st1 = Hello. In the first iteration, st2 is H. In the second iteration, e is placed in the first position, and H pushes to the last position. st1 = input("Please enter your own: ") st2 = '' for i in st1: st2 = i + st2 print("After = ", st2)
🌐
CodeScracker
codescracker.com › python › program › python-program-reverse-string.htm
Python Program to Reverse a String
Here are the list of programs covered in this article: ... The question is, write a Python program to find and print the reverse of a given string. The program given below is its answer: print("Enter the String: ", end="") str = input() strRev = str[::-1] str = strRev print("\nReverse =", str)
🌐
PREP INSTA
prepinsta.com › home › python program › program to reverse a string in python
Program to Reverse a String in Python | PrepInsta | Top 500 Codes
October 13, 2022 - Take string as input using input() function. Initialise an empty string output. Run a loop from last to first with step size as 1. Append the element to the output string using ‘+’ operator.
🌐
BeginnersBook
beginnersbook.com › 2018 › 06 › python-program-reverse-string
Python Program to Reverse a given String
# Program published on https://beginnersbook.com # Python program to reverse a given String # Using a user-defined function def reverse(str): s = "" for ch in str: s = ch + s return s # given string mystr = "BeginnersBook" print("Given String: ", mystr) # reversed string print("Reversed String: ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-reverse-string
Python Reverse String - 5 Ways and the Best One | DigitalOcean
August 3, 2022 - Hi, Thank you for the explanation. I also wanted to know how to add new lines to the output. For example I have made a reverse string input program: def reverse_slice(s): return s[::-1] s=input('enter a string ') print(s[::-1]) How would I a make my answer be on multiple lines?
Find elsewhere
🌐
Javatpoint
javatpoint.com › how-to-reverse-a-string-in-python
How to reverse a string in Python - Javatpoint
How to reverse a string in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.
🌐
Real Python
realpython.com › reverse-string-python
Reverse Strings in Python: reversed(), Slicing, and More – Real Python
July 31, 2023 - These features allow you to use slicing to directly generate a copy of a given string in reverse order. The second option is to use the built-in function reversed() to create an iterator that yields the characters of an input string in reverse order.
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › how to reverse a string in python
How to Reverse a String in Python - Shiksha Online
March 3, 2023 - Let’s understand how to reverse a string in Python using various methods: #Define a function def rev_func(string): # Declare a string variable revstr = '' #Iterate string with for loop for x in string: # Appending chars in reverse order revstr = x + revstr return revstr string = str(input()) #Print Original and Reverse string print('Original String: ', string) print('Reverse String: ', rev_func(string))
🌐
Quora
quora.com › How-do-you-write-a-Python-program-to-reverse-a-string
How to write a Python program to reverse a string - Quora
Answer: Simple. Text processing is one thing Python does real well:| We can use a list expression: >>> str = “Madam, I'm Adam" >>> str[: :-1) # (this is a Python idiom) “madA m'I ,madaM" The -1 says “start at the end of the string” There’s a function, reversed(str) - it returns an ...
🌐
w3resource
w3resource.com › python-exercises › python-conditional-exercise-5.php
Python Exercise: Reverse a word - w3resource
... # Prompt the user to input a word word = input("Input a word to reverse: ") # Iterate through the characters of the word in reverse order for char in range(len(word) - 1, -1, -1): # Print each character from the word in reverse order without ...
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-39.php
Python: Reverse a string - w3resource
... # Define a function 'reverse_string' that takes a string 'str1' as input. # The function returns the reversed version of the input string. def reverse_string(str1): return ''.join(reversed(str1)) # Print an empty line for spacing.
🌐
Quora
quora.com › How-do-you-reverse-a-string-the-user-has-inputed-in-Python
How to reverse a string the user has inputed in Python - Quora
Answer (1 of 26): [code ]# Python code to reverse a string [/code] [code ]# using loop [/code] [code ]def[/code] [code ]reverse(s): [/code] [code ] str[/code] [code ]=[/code] [code ]"" [/code] [code ] for[/code] [code ]i in[/code] [code ]s: [/code] [code ] str[/code] [code ]=[/code] [code ]...
🌐
Teachoo
teachoo.com › 17491 › 3904 › Question-6 › category › Past-Year---3-Mark-Questions
[Functions in Pyhton] Write a Python program to reverse a string
Defining a function to reverse ... string ‘rev_str’ is declared str1 is reversed by taking each element of str1 and assigning it to rev_str in reverse order Show More...
🌐
Python Guides
pythonguides.com › python-program-to-reverse-a-string
How To Reverse A String In Python?
January 12, 2026 - Using slicing ([::-1]) is the simplest way to reverse a string; it’s short, fast, and easy to read for both beginners and experienced programmers. If you want to know how to reverse a string in Python without slicing, then you can check this method and example.
🌐
GUVI
guvi.in › blog › python › python reverse string: 7 effective ways with examples
Python Reverse String: 7 Effective Ways with Examples
January 8, 2026 - This function reverse_string_using_stack takes a string as input, uses a list as a stack to reverse the string, and then returns the reversed string. The example demonstrates reversing the string “hello” to “olleh”. By understanding ...