Apply Now Apply Now Apply Now
header_logo
Post thumbnail
PYTHON

Python Reverse String: 6 Methods with Code and Speed Comparison

By Lukesh S

In the versatile world of programming, Python reverse string stands as a fundamental yet crucial skill that every coder should master.

Understanding how to reverse a string in Python not only sharpens your problem-solving skills but also opens up a myriad of possibilities for manipulating data and implementing algorithms.

This article delves into seven effective methods for reversing strings in Python, providing you with a comprehensive guide to tackle this common problem.

Table of contents


  1. TL;DR Summary
  2. Why Python Doesn't Have a Built-In String Reverse
  3. 6 Ways to Reverse a String in Python
    • Slicing with [::-1]
    • reversed() with join()
    • For Loop
    • join() with list.reverse()
    • Recursion
    • Stack (LIFO)
  4. Method Comparison In Python Reverse String: Time, Space, and Readability
  5. Reverse String Without Slicing: For Interview Coding Rounds
  6. Reverse Words in a String (Not Characters): A Different Problem
  7. Common Mistakes to Avoid
  8. Reverse String Python Interview Questions List
  9. Concluding Thoughts...
  10. FAQs
    • Can you use reverse () on a string?
    • What is the fastest way to reverse a string in Python?
    • How do you reverse a string without using slicing?
    • How do you reverse the words in a sentence instead of the characters?
    • What is the time complexity of reversing a string in Python?
    • Does reversing a string change the original string?

TL;DR Summary

  • Python strings don’t have a built in .reverse() method, so you need a workaround.
  • The fastest and most common method is slicing: text[::-1].
  • Other reliable options include reversed() with join(), a for loop, join() with list.reverse(), recursion, and a stack based approach.
  • All 6 methods run in O(n) time for most practical purposes, but they differ in readability, space usage, and interview suitability.
  • If you need to reverse the order of words instead of characters, that’s a different problem with a different solution.

Why Python Doesn’t Have a Built-In String Reverse

If you’ve searched for a .reverse() method for strings and come up empty, you’re not alone. Python strings are immutable, which means they can’t be changed in place the way lists can.

That’s exactly why list has a .reverse() method and string doesn’t. You’ll need one of the six approaches below instead, and picking the right one depends on whether you’re writing production code or preparing for a coding interview.

💡 Did You Know?

Python strings skip the .reverse() method entirely because they’re immutable. Mutable sequences like lists get in-place methods such as .reverse() and .append(), but strings always return a new object instead of modifying themselves.

6 Ways to Reverse a String in Python

6 Ways to Reverse a String in Python

1. Slicing with [::-1]

This is the method most Python developers reach for first. It’s short, readable, and doesn’t need a loop or import.

text = "GUVI"
reversed_text = text[::-1]
print(reversed_text)  # IVUG

2. reversed() with join()

reversed() returns an iterator, not a string, so you pair it with "".join() to rebuild the string.

text = "GUVI"
reversed_text = "".join(reversed(text))
print(reversed_text)  # IVUG

3. For Loop

Useful when you want to see exactly how the reversal happens, character by character.

def reverse_with_loop(text):
    result = ""
    for char in text:
        result = char + result
    return result

print(reverse_with_loop("GUVI"))  # IVUG

4. join() with list.reverse()

Here you convert the string to a list, reverse the list in place, then join it back.

def reverse_with_join(text):
    chars = list(text)
    chars.reverse()
    return "".join(chars)

print(reverse_with_join("GUVI"))  # IVUG

5. Recursion

This method breaks the string down until it hits an empty string, then rebuilds it in reverse as the calls return.

def reverse_recursive(text):
    if len(text) == 0:
        return text
    return reverse_recursive(text[1:]) + text[0]

print(reverse_recursive("GUVI"))  # IVUG
MDN

6. Stack (LIFO)

Push every character onto a stack, then pop them off. Since a stack is last in, first out, the popped order is automatically reversed.

def reverse_with_stack(text):
    stack = list(text)
    result = ""
    while stack:
        result += stack.pop()
    return result

print(reverse_with_stack("GUVI"))  # IVUG

If you want to build a stronger foundation before moving to advanced string and data structure problems, HCL GUVI’s Python Course covers this with hands on projects and placement support.

Method Comparison In Python Reverse String: Time, Space, and Readability

Method Comparison In Python Reverse String: Time, Space, and Readability
MethodTime ComplexitySpace ComplexityReadabilityWhen to Use
Slicing [::-1]O(n)O(n)HighEveryday scripts, quick fixes
reversed() + join()O(n)O(n)HighPythonic, iterator based style
For loopO(n²)*O(n)MediumLearning how reversal works internally
join() + list.reverse()O(n)O(n)MediumWhen you already have a list of characters
RecursionO(n)O(n)Low for beginnersPracticing recursive thinking
StackO(n)O(n)MediumDemonstrating LIFO logic in interviews
Method Comparison: Time, Space, and Readability

*The for loop version is O(n²) in the worst case because each concatenation (char + result) creates a brand new string, since strings are immutable. For short strings this rarely matters, but avoid it for large text.

Reverse String Without Slicing: For Interview Coding Rounds

Some interviewers specifically ask you to reverse a string without slicing, to check if you understand pointer based logic. Here’s the two-pointer swap approach, done on a list since strings can’t be modified in place.

def reverse_without_slicing(text):
    chars = list(text)
    left, right = 0, len(chars) - 1
    while left < right:
        chars[left], chars[right] = chars[right], chars[left]
        left += 1
        right -= 1
    return "".join(chars)

print(reverse_without_slicing("GUVI"))  # IVUG

You start with pointers at both ends of the list, swap the characters, then move the pointers toward the middle. This is the same logic used to check for palindromes, so it’s worth practicing before interviews.

Reverse Words in a String (Not Characters): A Different Problem

This one trips up a lot of beginners. Reversing a string flips the character order. Reversing the words in a sentence keeps each word intact but flips their order.

sentence = "Learn Python with GUVI"
reversed_words = " ".join(sentence.split()[::-1])
print(reversed_words)  # GUVI with Python Learn

Here, .split() breaks the sentence into a list of words, [::-1] reverses that list, and " ".join() puts it back together with spaces. If you’re asked this in an interview, clarify which version they mean before you start coding.

Common Mistakes to Avoid

  1. Assuming strings have a .reverse() method. Only lists have it. Calling .reverse() on a string raises an AttributeError. Use slicing or reversed() instead.
  2. Using string concatenation inside a loop for long text. Since strings are immutable, each concatenation builds a new string. For large inputs, this gets slow. Prefer join() or slicing.
  3. Mixing up “reverse a string” with “reverse the words in a string.” These are two different problems with two different solutions, and interviewers often ask both.
  4. Forgetting the base case in recursion. Skipping it for an empty string causes infinite recursion and a RecursionError.

Also read: Mastering Recursion in Python: A Comprehensive Guide

Reverse String Python Interview Questions List

  • How do you reverse a string in Python without using slicing?
  • What’s the time complexity difference between slicing and a for loop?
  • How would you reverse only the words in a sentence, not the characters?
  • Can you use .reverse() on a string? Why not?
  • How do you check if a string is a palindrome using reversal?
  • What’s the difference between reversed() and [::-1]?
  • How would you reverse a string using recursion, and what’s its space complexity?

Kickstart your Programming journey by enrolling in HCL GUVI’s Python Course where you will master technologies like multiple exceptions, classes, OOPS concepts, dictionaries, and many more, and build real-life projects.

Concluding Thoughts…

Reversing a string in Python comes down to picking the right tool for the job. Slicing and reversed() handle most everyday cases with clean, readable code. Loops, recursion, and stacks matter more for interviews, where they show you understand what’s happening under the hood.

Once you’re comfortable with these six methods, try the word reversal problem and the no-slicing version too. Together, they cover almost every version of this question you’ll run into, whether it’s a coding assignment or a technical interview round.

FAQs

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.

What is the fastest way to reverse a string in Python?

Slicing (text[::-1]) is generally the fastest and most readable option for typical string lengths.

How do you reverse a string without using slicing?

Use a two-pointer swap on a list of characters, or use reversed() with join(). This is common in interview rounds that restrict slicing.

How do you reverse the words in a sentence instead of the characters?

Split the sentence into words, reverse that list, then join it back with spaces: " ".join(sentence.split()[::-1]).

What is the time complexity of reversing a string in Python?

Most methods, including slicing, reversed(), and the stack based approach, run in O(n) time, where n is the length of the string.

MDN

Does reversing a string change the original string?

No. Strings in Python are immutable, so every method here returns a new string instead of modifying the original.

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Get in Touch
Chat on Whatsapp
Request Callback
Share logo Copy link
Table of contents Table of contents
Table of contents Articles
Close button

  1. TL;DR Summary
  2. Why Python Doesn't Have a Built-In String Reverse
  3. 6 Ways to Reverse a String in Python
    • Slicing with [::-1]
    • reversed() with join()
    • For Loop
    • join() with list.reverse()
    • Recursion
    • Stack (LIFO)
  4. Method Comparison In Python Reverse String: Time, Space, and Readability
  5. Reverse String Without Slicing: For Interview Coding Rounds
  6. Reverse Words in a String (Not Characters): A Different Problem
  7. Common Mistakes to Avoid
  8. Reverse String Python Interview Questions List
  9. Concluding Thoughts...
  10. FAQs
    • Can you use reverse () on a string?
    • What is the fastest way to reverse a string in Python?
    • How do you reverse a string without using slicing?
    • How do you reverse the words in a sentence instead of the characters?
    • What is the time complexity of reversing a string in Python?
    • Does reversing a string change the original string?