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
Top answer
1 of 14
3161

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.

🌐
Reddit
reddit.com › r/learnprogramming › why does [::1] reverse a string in python?
r/learnprogramming on Reddit: Why does [::1] reverse a string in Python?
September 21, 2023 -

For example:

txt = "Hello World"[::-1]

Isn't the splice syntax [start : stop: step]? And default of start and stop are the beginning and end of the string? So that would make the above start at the beginning, stop at the end, but step by -1. That feels like it would start at the beginning, then step backwards to...before the beginning of the string?

Sorry for the silly question, I just can't figure out why this syntax works the way it does.

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
1
February 20, 2025
Why does [::1] reverse a string in Python? : learnprogramming
🌐 r/learnprogramming
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
🌐 r/Python
8
0
December 4, 2021
What's the best way to reverse a string in Python?
Dunno about best, but using string splicing is an easy way to do it. s=s[::-1] It works by doing [start:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string More on reddit.com
🌐 r/Python
17
4
March 19, 2017
🌐
Hero Vired
herovired.com › learning-hub › topics › how-to-reverse-a-string-in-python
How to Reverse a String in Python - Hero Vired Topics
July 17, 2024 - In the above example, a while loop is used to iterate the last index of the initial string and a reverse string is created. ... Python provides an in-built operator to reverse a string known as the slicing operator. This operator creates a reversed ...
🌐
Scaler
scaler.com › topics › reverse-string-in-python
String reverse in Python
April 7, 2022 - Scaler Topics offers free certificate courses for all programming languages like Java, Python, C, C++, DSA etc. Learn from top MAANG experts and level up your skills. Explore now
🌐
Programiz
programiz.com › python-programming › methods › built-in › reversed
Python reversed()
string = 'Python' result = reversed(string) # convert the iterator to list and print it print(list(result)) # Output: ['n', 'o', 'h', 't', 'y', 'P']
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
1 week ago - Note that the exponent is written in decimal rather than hexadecimal, and that it gives the power of 2 by which to multiply the coefficient. For example, the hexadecimal string 0x3.a7p10 represents the floating-point number (3 + 10./16 + 7./16**2) * 2.0**10, or 3740.0: ... Applying the reverse conversion to 3740.0 gives a different hexadecimal string representing the same number:
Find elsewhere
🌐
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 ...
🌐
ReqBin
reqbin.com › code › python › hcwbjlmi › python-reverse-string-example
How do I reverse a string in Python?
December 20, 2022 - The easiest and fastest way to reverse a string in Python is to use the slice operator [start:stop:step]. When you pass a step of -1 and omit the start and end values, the slice operator reverses the string.
🌐
W3Schools
w3schools.com › c › c_strings.php
C Strings
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
Pierian Training
pieriantraining.com › home › how to reverse a string in python
How to Reverse a String in Python - Pierian Training
April 27, 2023 - For example, `my_string[::-1]` is equivalent to `my_string[None:None:-1]`. The `None` values represent the start and end indices of the slice, and since they are omitted, Python assumes that we want to include all characters in the string. Overall, using slicing is an easy and efficient way to reverse a string in Python.
🌐
dbader.org
dbader.org › blog › python-reverse-string
How to Reverse a String in Python – dbader.org
January 9, 2018 - An overview of the three main ways to reverse a Python string: “slicing”, reverse iteration, and the classic in-place reversal algorithm. Also includes performance benchmarks.
🌐
4Geeks
4geeks.com › how-to › how-to-reverse-string-in-python
How to reverse string in Python?
July 16, 2025 - One of the easiest ways to reverse a string in Python is by using string slicing.
🌐
Linode
linode.com › docs › guides › how-to-reverse-a-string-in-python
How to Reverse a String in Python | Linode Docs
May 13, 2022 - See our For and While Loops in Python 3 guide for deeper dive into these loop statements. A while loop takes a single condition and loops until that condition is no longer met. The standard approach is to then have the condition manipulated, within the loop, to end the loop when it is no longer needed. To reverse a string with a while loop, you can use a variable starting at the value of the string’s length.
🌐
Simplilearn
simplilearn.com › home › resources › software development › how to reverse a string in python: a definitive guide
How to Reverse a String in Python: A Definitive Guide
March 27, 2025 - Did you know that there is no built-in function to reverse a String in Python? Discover more about reversing strings, ways to reverse them and more now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Bhrighu
bhrighu.in › blog › reverse-a-string-in-python
How to Reverse a String in Python (5 Easy Methods)
Therefore, reversing a string involves creating a new string with characters in reverse order. This guide explores how to reverse a string in Python using various methods. From the efficient use of Python string slicing to traditional loop-based techniques, we'll cover everything you need to know.
🌐
Flexiple
flexiple.com › python › python-reverse-string
Reverse String In Python - Flexiple
March 18, 2024 - To reverse a string in Python using a loop, follow these steps. First, initialize an empty string that will hold the reversed string. Then, use a loop to iterate over the original string, adding each character to the beginning of the new string.
🌐
GitHub
github.com › mrexodia › ida-pro-mcp
GitHub - mrexodia/ida-pro-mcp: AI-powered reverse engineering assistant that bridges IDA Pro with language models through MCP. · GitHub
py_eval(code): Execute arbitrary Python code in IDA context (returns dict with result/stdout/stderr, supports Jupyter-style evaluation). analyze_funcs(addrs): Comprehensive function analysis (decompilation, assembly, xrefs, callees, callers, strings, constants, basic blocks).
Author   mrexodia
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string
Python String - GeeksforGeeks
Slicing is a way to extract a portion of a string by specifying the start and end indexes. The syntax for slicing is string[start:end], where start starting index and end is stopping index (excluded). Example: In this example we are slicing through range and reversing a string.
Published   January 14, 2026
🌐
Great Learning
mygreatlearning.com › blog › it/software development › how to reverse a string in python: the definitive guide
How to Reverse a String in Python: The Definitive Guide
August 25, 2025 - Python def reverse_string_loop(s): reversed_s = "" for char in s: reversed_s = char + reversed_s return reversed_s my_string = "hello" reversed_string = reverse_string_loop(my_string) print(reversed_string) # Output: "olleh" Initialize an empty string, reversed_s.