Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

How to Reverse a String in Python: Slicing, Loops, and Recursion

Use text[::-1] for concise Python string reversal. Compare it with reversed(), a character-by-character loop, and recursion.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a new string with its characters in reverse order, use Python’s slice step -1: text[::-1]. It is the concise default for ordinary Python strings. Use reversed() when you only need to iterate backward, a loop when you need to process characters as you go, and recursion mainly to demonstrate the concept.

Reverse a string with slicing

In Python 3.14, a string is a sequence, and slicing returns a string containing the selected characters. The slice notation is [start:stop:step]; a negative step walks backward. With both bounds omitted, text[::-1] traverses from the end to the beginning. See the Python sequence operations reference and the Python tutorial’s string section.

As an Amazon Associate I earn from qualifying purchases.

text = "Python"
reversed_text = text[::-1]
print(reversed_text)  # nohtyP

For reuse, wrap the expression in a function:

def reverse_string(text: str) -> str:
    return text[::-1]

The original string is not modified: Python strings are immutable, and the slice produces a new string. An empty string works naturally; reversing "" returns "".

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a method for how you will use the result

Method Best fit What it produces
text[::-1] Concise, ordinary string reversal A new string
reversed(text) Reading characters in reverse without needing a string immediately An iterator
Loop with a character list Learning traversal or applying per-character logic A new string after joining
Recursion Demonstrating a base case and recursive step on short input A new string, with recursive calls

Use reversed() to iterate backward

The built-in reversed() returns an iterator, not a string. Join its characters if a string result is required:

reversed_text = "".join(reversed(text))

If you only need to examine or process characters in reverse order, consume the iterator directly rather than creating a string. The Python FAQ recommends reversed(sequence) for reverse iteration, and the data structures tutorial shows reverse iteration techniques.

Use a loop to make each step visible

This version starts at the last valid index and counts backward, collecting characters in a list before joining them:

def reverse_with_loop(text: str) -> str:
    chars = []
    for index in range(len(text) - 1, -1, -1):
        chars.append(text[index])
    return "".join(chars)

The range stops before -1, so index 0 is included. Building a list and joining once avoids repeatedly constructing a growing string inside the loop. Choose this form when the traversal itself matters or each character needs additional handling; otherwise slicing is shorter.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recursion is useful for a lesson, not a general-purpose default

A recursive definition reverses the tail, then places the first character at the end:

def reverse_recursive(text: str) -> str:
    if text == "":
        return ""
    return reverse_recursive(text[1:]) + text[0]

The empty string is the base case; each recursive call receives a shorter tail. This example is suitable for short inputs when teaching recursion. It creates calls proportional to the input length and performs slicing and concatenation along the way, so it is not a practical choice for arbitrarily long strings.

Python’s interpreter limits recursion depth to help prevent runaway recursion from overflowing the C stack. The safe limit depends on the platform; the sys documentation cautions that setting the limit too high can crash the process. Raising it casually is not a fix for this reversal approach.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What “reverse” means for Python strings

These techniques reverse the order of elements in a Python str. They should not be assumed to preserve every user-perceived visual character as a unit: some displayed characters are made from multiple Unicode code points, such as a base letter followed by a combining mark. If an application needs to reverse visual text units rather than Python string elements, it needs text-segmentation handling beyond the methods shown here.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.