🌐
Medium
medium.com β€Ί @thesumitshrestha β€Ί day-11-reverse-a-string-a1fc352c7e78
Day 11 | Reverse a String
March 11, 2024 - For example, if we have the string β€œhello”, using the Two Pointers Technique, we start with two pointers, one at the β€˜h’ and one at the β€˜o’. They swap positions, then the pointers move towards each other until they meet, resulting in the reversed string β€œolleh”. Since we are given as a list the output will be a list. So, we need to convert [β€˜h’, β€˜e’, β€˜l’, β€˜l’, β€˜o’] to [β€˜o’, β€˜l’, β€˜l’, β€˜e’, β€˜h’]. Here is the python implementation.
🌐
DevsCall
devscall.com β€Ί blog β€Ί reverse-string
Reverse a String in Python | DevsCall
July 17, 2024 - Initialize two pointers: left at the start of the string and right at the end. ... Swap the characters at left and right. Move left one step to the right and right one step to the left.
🌐
Magica
magica.com β€Ί youtube-summarizer β€Ί mastering-the-reverse-string-problem-in-python-using-two-pointers-eeu0z-ISr3Y
Mastering the Reverse String Problem in Python Using Two Pointers | Magica
The time complexity of this algorithm is O(n), where n is the length of the string, as we iterate through the string once. The space complexity is O(1) since we are not using any additional space for another array, adhering to the problem's constraints. Here is the Python code that implements the two-pointer solution: def reverse_string(s): left = 0 right = len(s) - 1 while left < right: # Swap characters hold = s[left] s[left] = s[right] s[right] = hold # Move pointers left += 1 right -= 1 return s
🌐
TutorialsPoint
tutorialspoint.com β€Ί reverse-string-in-python
Reverse String in Python
April 28, 2020 - We have to reverse the string without ... β€˜L’, β€˜L’, β€˜E’, β€˜H’] To solve this, we will follow these steps βˆ’ Β· Take two pointers to start = 0 and end = length of the string – 1 Β·...
🌐
YouTube
youtube.com β€Ί watch
Reverse String - Leetcode 344 - 2 Pointers - (Python) - YouTube
Master Data Structures & Algorithms for FREE at https://AlgoMap.io/Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: h...
Published Β  April 17, 2024
🌐
LeetCode
leetcode.com β€Ί problems β€Ί reverse-string β€Ί solutions β€Ί 1427801 β€Ί easy-solution-with-two-pointer
Reverse String python solution (two pointer method)
August 28, 2021 - Can you solve this real interview question? 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.
🌐
YouTube
youtube.com β€Ί watch
Reverse String Python Leetcode 344 | Two Pointers ...
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published Β  July 6, 2020
🌐
YouTube
youtube.com β€Ί watch
Reverse String (Two Pointers Approach ) | Leet Code - YouTube
Thanks if u r watching us.....#Dev19 #Vk #Vaibhav18
Published Β  April 6, 2020
Find elsewhere
🌐
Interviewing.io
interviewing.io β€Ί questions β€Ί reverse-string
How to Reverse a String [Interview Question + Solution]
September 13, 2018 - Initialize two pointers, start and end, to point at the start and the end of the string, respectively. Loop until start is less than end. Swap the characters at start and end. Increment start and decrement end.
🌐
AlgoMonster
algo.monster β€Ί liteproblems β€Ί 344
344. Reverse String - In-Depth Explanation
The solution uses a two-pointer technique where one pointer i starts at the beginning of the array (index 0) and another pointer j starts at the end (index len(s) - 1). The algorithm repeatedly swaps the characters at positions i and j, then moves i forward and j backward.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί dsa β€Ί reverse-a-string
Reverse a String - GeeksforGeeks
Start from both ends of the string and keep swapping characters while moving toward the center. 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
🌐
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_while(s): ... which is mutable. Initialization: Create two "pointers," left starting at the first index (0) and right starting at the last index (len(s) - 1)....
🌐
DEV Community
dev.to β€Ί shahrouzlogs β€Ί day-1-mastering-the-two-pointer-string-reversal-2p14
🐍 Day 1: Mastering the Two-Pointer String Reversal - DEV Community
October 23, 2025 - def reverse_string_pointers(text): # Convert to list for in-place swapping text_list = list(text) left, right = 0, len(text) - 1 while left < right: # Pythonic swapping text_list[left], text_list[right] = text_list[right], text_list[left] left += 1 right -= 1 # Convert back to string and return return "".join(text_list) Focusing on how an algorithm works, rather than just getting the right answer, truly reinforces logical thinking. The two-pointer method is foundational, and practicing it with a simple challenge was a perfect start to the series.
🌐
Medium
medium.com β€Ί @begunova β€Ί reverse-strings-with-python-3a841058fb5a
Reverse Strings with Python. QA/SDET Interview Questions
March 25, 2024 - class Solution: def reverseVowels(self, ... - where n is the length of the input string s. The two pointers i and j move towards each other, and each character is processed once....
🌐
Medium
medium.com β€Ί @KeyurRamoliya β€Ί python-use-two-pointer-technique-for-efficient-array-and-string-manipulation-10b6b09f8293
Python β€” Use Two-Pointer Technique for Efficient Array and String Manipulation | by Keyur Ramoliya | Medium
November 14, 2023 - The Two-Pointer Technique involves using two-pointers that traverse an array or string to solve problems efficiently. It is especially useful for tasks like searching for pairs, reversing elements, or checking for specific conditions.
🌐
InterviewBit
interviewbit.com β€Ί coding problems β€Ί reverse string (c++, java, and python)
Reverse String (C++, Java, and Python) - InterviewBit
June 23, 2023 - def reverseString(self, s): def helper(left, right): if left < right: s[left], s[right] = s[right], s[left] helper(left + 1, right - 1) helper(0, len(s) - 1) Time Complexity:O(N), where N is the length of the string. Space Complexity:O(N), since the recursion stack takes space. Another simple approach to solve this problem is to use two pointers method.
🌐
TechieFreak
techiefreak.org β€Ί home β€Ί blogs β€Ί string β€Ί reverse a string
Reverse a String in C, C++, Java, Python & JavaScript
April 9, 2026 - This approach uses the two-pointer technique. ... Convert the string into a character array (if needed). ... Swap characters at left and right. ... Continue until left < right. ... #include #include void reverseString(char str[]) { int left ...
🌐
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.
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.