🌐
LearnYard
read.learnyard.com › dsa › reverse-string
Reverse String
June 8, 2025 - For C++ refer to this Link : https://cplusplus.com/reference/algorithm/reverse/ For Python refer to this Link (In python we use the string slicing) : https://www.w3schools.com/python/python_strings_slicing.asp · For JAVA refer to this Link: https://www.codecademy.com/resources/docs/java/stringbuilder/reverse ·
Discussions

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
What is the most efficient way to reverse a string in Python? - Stack Overflow
I'm looking for the most efficient method to reverse a string in Python. While there are several approaches to accomplish this task, I want to know which technique provides the best performance in ... More on stackoverflow.com
🌐 stackoverflow.com
Fastest way to reverse a string - and it's not extended string splicing?
I was told there's a faster method with some optimisations possible. If reversing strings is a bottleneck needing optimization, then your codebase might have bigger issues. More on reddit.com
🌐 r/learnpython
70
245
August 12, 2020
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
🌐 r/learnprogramming
15
12
September 21, 2023
People also ask

Can you use reverse on a string in Python
divPython provides a builtin function called reversed which can be used to reverse the characters in a stringdiv
🌐
scholarhat.com
scholarhat.com › home
How to Reverse a String in Python
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
Is there a reversed in Python
divIs there a reverse function in Python Yes the reversed function allows us to reverse the order of items in a sequencenbspdiv
🌐
scholarhat.com
scholarhat.com › home
How to Reverse a String in Python
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-reverse-string
Python Reverse String - 5 Ways and the Best One | DigitalOcean
August 3, 2022 - $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_slicing("ABç∂EF"*10)' 100000 loops, best of 5: 0.449 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_list("ABç∂EF"*10)' 100000 loops, best of 5: 2.46 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_join_reversed_iter("ABç∂EF"*10)' 100000 loops, best of 5: 2.49 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.
🌐
Medium
medium.com › better-programming › benchmarking-the-best-way-to-reverse-a-string-in-python-9c73d87b1b1a
Benchmarking the Best Way to Reverse a String in Python | by Nick Gibbon | Better Programming
September 16, 2019 - 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.
🌐
Javatpoint
javatpoint.com › how-to-reverse-a-string-in-python
How to reverse a string in Python - Javatpoint
Write the Python Program to Reverse the Vowels in the Given String · How to use Pass statement in Python · Recursion in Python · Real-Time Data Analysis from Social Media Data in Python · Exception handling in Python · Least Recently Used Page Replacement Algorithm Python Program ·
🌐
Educative
educative.io › answers › how-do-you-reverse-a-string-in-python
How do you reverse a string in Python?
In Python, strings are ordered sequences of character data. There is no built-in method to reverse a string.
Find elsewhere
🌐
ScholarHat
scholarhat.com › home
How to Reverse a String in Python
September 11, 2025 - Only 10% of learners become Python experts. 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.
🌐
Real Python
realpython.com › lessons › python-reverse-string-custom-algorithm
Reversing a String Using a Custom Algorithm (Video) – Real Python
For example, let’s say you want to reverse the string "HELLO". During the first iteration, the "H" and "O" would be swapped, as the left pointer is equal to 0, and the right pointer is equal to 4. During the second iteration, the left pointer has now been incremented to 1, with the right having been decremented to 3. Therefore, the "E" and the "L" would be swapped. During the third and final iteration, the left and right pointers would both equal 2, and therefore the algorithm would exit, returning the string, "OLLEH".
Published   August 1, 2023
🌐
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 - Learn how to reverse a string in Python using loops, slicing, recursion, stack, and more. Explore challenges and multiple methods.
🌐
W3Schools
w3schools.com › python › ref_func_reversed.asp
Python reversed() Function
The list.reverse() method reverses a List. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, ...
🌐
Unstop
unstop.com › home › blog › how to reverse a string in python in 10 ways! (code)
How To Reverse A String In Python In 10 Ways! (Code)
December 21, 2023 - There are multiple ways to reverse a string in Python, including the slice operator, extended slicing, reverse() method, loops, recursion, stack, etc.
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.

🌐
Real Python
realpython.com › reverse-string-python
Reverse Strings in Python: reversed(), Slicing, and More – Real Python
July 31, 2023 - In this step-by-step tutorial, you'll learn how to reverse strings in Python by using available tools such as reversed() and slicing operations. You'll also learn about a few useful ways to build reversed strings by hand.
🌐
GeeksforGeeks
geeksforgeeks.org › reverse-string-python-5-different-ways
How to reverse a String in Python - GeeksforGeeks
Looping & List comprehension provides more control over the reversal process. stack approach is least suitable here, but it helps in understanding the data structures & algorithmic thinking and problem-solving. ... A string is a sequence of characters. Python treats anything inside quotes as a string.
Published   November 21, 2024
🌐
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.