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
๐ŸŒ
Online String Tools
onlinestringtools.com โ€บ reverse-string
Reverse a String โ€“ Online String Tools
This tool reverses the input string. If you enable the multi-line mode, then it reverses every string on every line. You can also trim strings before reversing them. Trimming is an operation that removes the whitespace characters from the beginning and end of a string.
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ reverse-string
Reverse String - LeetCode
Reverse String - Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm] with O(1) extra memory.
Discussions

Method for reversing strings - Ideas - Discussions on Python.org
There may be other methods like splitting the string, reversing the resulting list, and then joining it back, but thatโ€™s a bit of work! There have been several times in my QA career where I am scripting in Python and need to reverse a string, but I have to look up the [::-1] syntax because ... More on discuss.python.org
๐ŸŒ discuss.python.org
2
February 20, 2025
How to reverse a string in c without using strrev?
You have string[begin] = '\0' where it should be output[begin] = '\0' More on reddit.com
๐ŸŒ r/C_Programming
9
1
September 9, 2019
How do I reverse a string in Python? - Stack Overflow
There is no built in reverse method for Python's str object. How can I reverse a string? More on stackoverflow.com
๐ŸŒ stackoverflow.com
programming languages - What do you use string reversal for? - Software Engineering Stack Exchange
In PHP it's strrev(), in Rails it's .reverse, but most languages don't have any string reverse function. Some have array reverse functions that can be used on characters. I was thinking this must b... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
December 8, 2010
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ reverse-a-string
Reverse a String - GeeksforGeeks
Each swap places the correct character in its reversed position, and when both pointers meet in the middle, the entire string becomes reversed.
Published ย  March 7, 2026
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Method for reversing strings - Ideas - Discussions on Python.org
February 20, 2025 - There may be other methods like splitting the string, reversing the resulting list, and then joining it back, but thatโ€™s a bit of work! There have been several times in my QA career where I am scripting in Python and need to reverse a string, but I have to look up the [::-1] syntax because ...
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how to reverse a string in c without using strrev?
How to reverse a string in c without using strrev? : r/C_Programming
September 9, 2019 - In the end, we explicitly add the end of the character symbol in the string. In the end, we print the reverse string. //Using Recursion In this, we will try to reverse the string using the recursive method. Recursion is a method in which a function gives a call to itself.
๐ŸŒ
YouTube
youtube.com โ€บ watch
14 Ways to Reverse a String! (And solve the exercise on Exercism) - YouTube
Explore 14 different ways to reverse a string. We look at the built-in functions and methods, dig into Runes and Graphemes, look at Stack vs Heap allocations...
Published ย  January 23, 2024
Find elsewhere
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.

๐ŸŒ
R-bloggers
r-bloggers.com โ€บ r bloggers โ€บ four ways to reverse a string in r
Four ways to reverse a string in R | R-bloggers
May 17, 2019 - This is the slowest method that will be shown, but it does get the job done without needing any packages. In this example, we use strsplit to break the string into a vector of its individual characters. We then reverse this vector using rev.
๐ŸŒ
Gastonsanchez
gastonsanchez.com โ€บ r4strings โ€บ reversing.html
17 Reversing Strings | Handling Strings with R
Our first example has to do with reversing a character string. More precisely, the objective is to create a function that takes a string and returns it in reversed order. The trick of this exercise depends on what we understand with the term reversing. For some people, reversing may be understood as simply having the set of characters in reverse order.
๐ŸŒ
Browserling
browserling.com โ€บ tools โ€บ text-reverse
Reverse Text - Reverse String - Online - Browserling Web Developer Tools
Useful, free online tool that reverses strings and text. No ads, nonsense, or garbage, just a text reverser. Press a button โ€“ get the result.
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ different-ways-of-reversing-a-string-in-cpp
Different ways of reversing a string in C++ | Codecademy
Original String: Hello, World! ... In the example, the loop reverses the string by iterating from the beginning to the midpoint. The swap() function, provided by the C++ Standard Library (std::swap()), exchanges the values of two variables.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ reverse-a-string-in-java
Reverse a String in Java - GeeksforGeeks
A string reversal means changing the order of characters from beginning to end in the opposite direction.
Published ย  May 12, 2026
๐ŸŒ
CodeChef
codechef.com โ€บ practice โ€บ course โ€บ strings โ€บ STRINGS โ€บ problems โ€บ PALINDRCHECK
Reverse Words in a String Practice Problem in Strings
Test your knowledge with our Reverse Words in a String practice problem. Dive into the world of strings challenges at CodeChef.
๐ŸŒ
Interviewing.io
interviewing.io โ€บ questions โ€บ reverse-string
How to Reverse a String [Interview Question + Solution]
September 13, 2018 - We can loop through each character of the original string and build the reversed string iteratively. We start with an empty string and append the characters to it as we loop across the original string. Please note that we are appending the characters to the beginning of the string.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb
Three Ways to Reverse a String in JavaScript
March 14, 2016 - The split() method splits a String object into an array of string by separating the string into sub strings. The reverse() method reverses an array in place.