String Slicing (Most Pythonic and Efficient)

The most concise and efficient way to reverse a string in Python is using slicing with [::-1]. This method leverages Python’s extended slice syntax to traverse the string backward from the last character to the first.

text = "Hello, World!"
reversed_text = text[::-1]
print(reversed_text)  # Output: !dlroW ,olleH
  • [::-1] means: start from the end, go to the beginning, stepping backward by 1.

  • It creates a new reversed string without modifying the original.

  • This is the recommended approach for most use cases due to its readability and performance.

Using reversed() and join()

This method is more explicit and breaks the reversal into two clear steps: reversing the characters and joining them into a string.

text = "Hello, World!"
reversed_text = ''.join(reversed(text))
print(reversed_text)  # Output: !dlroW ,olleH
  • reversed(text) returns an iterator of characters in reverse order.

  • ''.join() combines the characters into a single string.

  • Useful when you want to emphasize the logic or are working with iterators.

Using a For Loop

This method manually builds the reversed string by prepending each character from the original string.

text = "Hello, World!"
reversed_text = ""
for char in text:
    reversed_text = char + reversed_text
print(reversed_text)  # Output: !dlroW ,olleH
  • Simple to understand but less efficient for long strings due to repeated string concatenation (strings are immutable in Python).

  • Avoid for large strings; use a list and join() instead for better performance.

Using List Comprehension

A compact one-liner that generates characters in reverse order using indices.

text = "Hello, World!"
reversed_text = ''.join([text[i] for i in range(len(text)-1, -1, -1)])
print(reversed_text)  # Output: !dlroW ,olleH
  • range(len(text)-1, -1, -1) generates indices from last to first.

  • The list comprehension collects characters in reverse order.

  • Efficient and readable for those familiar with list comprehensions.

Using Recursion

A functional programming approach that calls the function recursively until the base case is reached.

def reverse_string(s):
    if len(s) <= 1:
        return s
    return reverse_string(s[1:]) + s[0]

text = "Hello, World!"
print(reverse_string(text))  # Output: !dlroW ,olleH
  • Elegant but not efficient for long strings due to repeated slicing and function calls.

  • Best for learning recursion, not production code.

Using a Stack (LIFO)

Simulates a stack using a list, where the last character added is the first removed.

text = "Hello, World!"
stack = list(text)
reversed_text = ""
while stack:
    reversed_text += stack.pop()
print(reversed_text)  # Output: !dlroW ,olleH
  • Uses the Last-In-First-Out (LIFO) principle.

  • Efficient and intuitive for understanding how stacks work.

Performance Summary

MethodSpeedReadabilityUse Case
[::-1]✅ Fastest✅ HighestDefault choice
join(reversed())✅ Fast✅ HighWhen clarity is key
For loop❌ Slow (for large strings)✅ HighLearning or small strings
List comprehension✅ Fast✅ MediumOne-liner preference
Recursion❌ Slow✅ MediumLearning only
Stack✅ Fast✅ MediumUnderstanding data structures

Best Practice: Use text[::-1] for most cases. It’s fast, clean, and idiomatic Python.

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

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
Why does [::1] reverse a string in Python? : learnprogramming
🌐 r/learnprogramming
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 18, 2017
Can someone explain this reverse string in Python?

You already have an explanation here, but you can run your short code through this nice visual debugger and see it happening step-by-step.

More on reddit.com
🌐 r/learnprogramming
3
1
February 10, 2013
People also ask

How to reverse a string in Python without a reverse function?
You can reverse a string in Python using slicing: reversed_string = original_string[::-1].
🌐
guvi.in
guvi.in › blog › python › python reverse string: 7 effective ways with examples
Python Reverse String: 7 Effective Ways with Examples
What is the fastest way to reverse a string in Python
divSlicing is the fastest and most efficient technique for python reverse string It uses a slice that takes a backward step 1div
🌐
scholarhat.com
scholarhat.com › home
How to Reverse a String in Python
Can you use reverse () on a string?
No, the reverse() The method cannot be used on a string in Python, as it is specifically designed for lists.
🌐
guvi.in
guvi.in › blog › python › python reverse string: 7 effective ways with examples
Python Reverse String: 7 Effective Ways with Examples
🌐
Python.org
discuss.python.org › ideas
Method for reversing strings - Ideas - Discussions on Python.org
February 20, 2025 - I would like to add a .reverse() method for strings. I think most modern languages have something like that and [::-1] is a bit archaic with little charm. There may be other methods like splitting the string, reversing t…
🌐
Better Programming
betterprogramming.pub › benchmarking-the-best-way-to-reverse-a-string-in-python-9c73d87b1b1a
Benchmarking the Best Way to Reverse a String in Python
September 16, 2019 - The complexity of the appending operation depends on the underlying implementation in the interpreter. Because Python strings are immutable, it is likely that each reversed_output = reversed_output + s[i] takes the current state of the output string and the new character and copies them to a new variable.
Find elsewhere
🌐
Replit
replit.com › home › discover › how to reverse a string in python
How to reverse a string in Python
1 month ago - It tells Python to move backward through the string one character at a time. By leaving the start and stop indexes empty, you're essentially telling it to include the entire string in this backward traversal. This creates a new, reversed copy of the string without altering the original.
🌐
GUVI
guvi.in › blog › python › python reverse string: 7 effective ways with examples
Python Reverse String: 7 Effective Ways with Examples
June 6, 2024 - When applied to strings, this method enables you to access individual characters or substrings efficiently. To reverse a string using slicing, you utilize the syntax a_string[start:stop:step].
🌐
ScholarHat
scholarhat.com › home
How to Reverse a String in Python
September 11, 2025 - Be one with our Free Python Online Course—start your journey now! Using Slicing ([::-1]): Slicing is the most efficient method to reverse a string in Python.
🌐
LeetCode
leetcode.com › problems › reverse-string
Reverse String - LeetCode
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.
🌐
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.
🌐
Reddit
reddit.com › r/python › 7 proven methods to reverse the python string in 2021
r/Python on Reddit: 7 proven methods to reverse the python string in 2021
December 4, 2021 -

How to reverse the python string now in 2021?

Hello to all python buddies,

You're stirring your cofee, and going to read r/Python. And you love the blog post.

Today, I'm going to make r/Python more lovable to you.

I'm going to show you the 6 proven methods to reverse the python string. Which are easy and quick to do.

So, start these methods

☺️

  1. Reverse the string using slice method

You can reverse the string using slice method.

The slice indicates the [start:end] position.

A start is a position where sequence start. and end is the position where sequence ends.

The first position is 0th index.

So, here you can use [::-1].

The [::-1] means sequence starting from last of the string.

For example,

a = ["hello"]

print(a[::-1])

It'll reverse the python string.

>>> olleh

2. Reversed the string using reversed() &join() methods

First of all, the reversed() method reverse the sequence.

After reversed() with you can join() every iterables as string.

Basically, the join() method join the iterables as a string seperator.

reversed() & join()

After running, this code you'll get something like

👇

output

3. Reversed the string: join() and sorted() method

As you know, sorted() sort the string or sequences in ascending or descending method.

Here, I'm going to use descending order.

For descending order, pass reverse = True inside sorted().

And previously, I've told that join joins the sequences as a string seperator.

For example,

join() & sorted()

Here, you can see that first I've sorted the string in descending order.

After that, I've join every character as a string.

When you run above code, you'll get:--->

output

So, you've get the reversed string as output.

4. Reversed the string using for loop

You can reverse the string using for loop.

To create the reverse string in for loop, you need function with empty string.

The every new string add to the empty string.

After adding, all the string it becomes the reverse string.

For example,

code

After running code, you'll get--->

output

So, here you've seen how to reverse the python string. I've told you the 6 methods.

And here I've shown you the 4 methods.

But I'm going to show you 3 methods more.

That means 7 method for reverse the python string.

So, I've given you 1 bonus method.

To get these 3 methods, check out the

👇

https://www.heypython.com/python-programming/reverse-the-python-string/

🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › reverse-string-python-5-different-ways
How to reverse a String in Python - GeeksforGeeks
We can reverse the string by taking a step value of -1. ... Python provides a built-in function called reversed() which can be used to reverse the characters in a string.
Published   3 days ago
🌐
Plain English
python.plainenglish.io › how-to-reverse-a-string-in-python-893bd713da4a
How to Reverse a String in Python? | by Ramandeep Ladhar | Python in Plain English
February 9, 2022 - In this article, we deal with the question “How to reverse a string in Python?” At last, we can conclude that slicing is the best way to reverse a string in python. Its code is simple and moreover, has fewer lines of operation.
🌐
Analytics Vidhya
analyticsvidhya.com › home › 5 ways to reverse a string in python
How to Reverse a String in Python in 5 Ways | Reverse Function
February 5, 2025 - In this article, we’ll review five distinct approaches to string reversal in Python, each with pros and cons. Starting with the simplest and most direct method—slicing to reverse the string—we’ll move on to more complex strategies, such as employing built-in functions and recursion.
🌐
Quora
quora.com › How-do-you-reverse-a-string-in-Python-like-hello-world-to-world-hello
How to reverse a string in Python, like 'hello world' to 'world hello' - Quora
Answer (1 of 13): To reverse a string word by word, you first have to turn it into a sequence of words, reverse that sequence, and turn it back into a single string. The first part is exactly what the split method on strings does: [code]>>> s = 'hello world' >>> s.split() ['hello', 'world'] [/c...
🌐
LogRocket
blog.logrocket.com › home › 5 methods to reverse a python string
5 methods to reverse a Python string - LogRocket Blog
June 4, 2024 - new_string = '' ... count = len(input_string) - 1 ... while count >= 0: ... new_string = new_string + input_string[count] ... count = count - 1 ... return new_string >>> w_reverse('?uoy era woH') 'How are you?' Here, we are creating a function and initializing a new variable, the same as the previous example · Now we take the length of the input string and subtract it by 1 because the index in Python starts from 0.