Placement Prep

How to Reverse a String in Python: 5 Methods Explained

Five ways to reverse a string in Python: slicing, reversed(), loop, recursion, and word-by-word, with code, complexity, and placement-round tips.

By FACE Prep Team 4 min read
python string-reversal placement-prep python-programs coding-interview technical-python

Reversing a string is one of the most common warm-up questions in Python placement coding rounds, and knowing at least three methods, not just the slice shortcut, is what separates a confident answer from a shaky one.

Why string reversal appears in placement tests

TCS NQT, AMCAT Automata, and CoCubes include string manipulation in their coding sections. String reversal is a frequent opening question because it checks three fundamentals at once: understanding of Python’s string model, familiarity with built-in functions, and the ability to write a loop that traverses a sequence correctly.

The questions come in two variants that test-takers often confuse:

  • Full-string reversal: "Python" becomes "nohtyP".
  • Word-by-word reversal: "Python program" becomes "nohtyP margorp" (each word reversed, word order unchanged).

Both variants appear on the same tests. Knowing the distinction before you sit the paper saves time.

For a broader view of which Python programs appear in placement coding rounds, see Python basic programs for practice.

Method 1: Slice notation

Python’s extended slice syntax accepts a third argument as the step. A step of -1 means “read from the end, moving backward by one character at a time.”

s = "faceprep"
reversed_s = s[::-1]
print(reversed_s)  # perpecaf
  • Time complexity: O(n), reads every character once.
  • Space complexity: O(n), creates a new string of the same length.

This is the expected answer in most placement coding rounds. It is concise, readable, and standard Python. If the question asks for “the simplest method,” slice notation is the answer.

One point interviewers follow up with: s[::-1] does not modify s. Python strings are immutable, so the original string is untouched after the operation.

Method 2: Using reversed() and join()

The built-in reversed() function returns an iterator that yields characters from the end of the sequence. Wrapping it with ''.join() collects them into a new string.

s = "faceprep"
reversed_s = "".join(reversed(s))
print(reversed_s)  # perpecaf
  • Time complexity: O(n).
  • Space complexity: O(n).

This approach is useful when an interviewer asks you to avoid slicing syntax. It also demonstrates awareness of iterators, a concept that comes up in follow-up questions about generators and lazy evaluation.

Note: reversed() requires the input to support len() and __getitem__. Strings satisfy both, so it works without any conversion.

Method 3: Manual loop reversal

When an interviewer says “no built-in functions, no slicing,” a loop that counts backward through the string indices is the answer.

def reverse_string(s):
    result = ""
    for i in range(len(s) - 1, -1, -1):
        result += s[i]
    return result

print(reverse_string("faceprep"))  # perpecaf

Step-by-step trace for input "faceprep" (length 8):

  • i = 7p

  • i = 6e

  • i = 5r

  • i = 4p

  • i = 3e

  • i = 2c

  • i = 1a

  • i = 0f

  • Result: "perpecaf"

  • Time complexity: O(n), one pass through the string.

  • Space complexity: O(n), builds a new string character by character.

A small performance note: repeated string concatenation with += inside a loop creates a new string object at each step, which can be slower than joining a list for very long strings. For placement test inputs the difference is irrelevant, but it is worth mentioning if the interviewer asks about optimisation.

Method 4: Reversing each word in a string

This is a separate problem from full-string reversal, and placement tests include both. The task: given "Python program", produce "nohtyP margorp" (each word reversed individually, word order preserved).

def reverse_each_word(s):
    words = s.split(" ")
    reversed_words = [word[::-1] for word in words]
    return " ".join(reversed_words)

print(reverse_each_word("Python program"))  # nohtyP margorp

How it works:

  • s.split(" ") splits "Python program" into ["Python", "program"].
  • The list comprehension applies [::-1] to each word: ["nohtyP", "margorp"].
  • " ".join(...) reassembles them with a space separator.

This approach handles any number of words. The same slice-per-word logic applies to punctuation-free input; for real-world text with punctuation attached to words, you’d need to strip and re-attach punctuation separately.

Understanding character-level string checks is useful context here; placement tests sometimes ask you to reverse only alphabetic characters and leave digits or special characters in place.

Method 5: Recursive reversal

Recursion appears as a follow-up in technical interviews. The base case is a string of length 0 or 1 (already reversed). The recursive step takes the first character and appends it after reversing the rest of the string.

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

print(reverse_recursive("faceprep"))  # perpecaf
  • Time complexity: O(n), one recursive call per character.
  • Space complexity: O(n), one stack frame per character.

Python’s default recursion limit is 1,000, so this fails for strings longer than around 990 characters. Interviewers use it to test recursive thinking, not as a production solution. If asked “is this safe for all inputs?” the correct answer is no, and the fix is to increase the recursion limit or switch to an iterative method.

Which method to use in a placement round

ScenarioBest method
”Reverse the string” with no constraintss[::-1]
Interviewer asks “without slicing”"".join(reversed(s))
”Write a loop, no built-ins”Manual loop with range()
”Reverse each word”split, slice each word, join
Follow-up asks for recursionRecursive function with base case

For sorting a string alphabetically, the same slicing and joining techniques apply, so practising this set of methods covers both problems efficiently.

Taking string operations further

String reversal is the first type of sequence-manipulation problem you’ll encounter in placement prep. The slice notation, reversed(), and join pattern that you just practised carries directly into text-processing tasks: cleaning prompts before sending them to an LLM, parsing model output, and inspecting tokenised text.

If the word-reversal variant or the recursive approach above sparked questions you want to test live, TinkerLLM’s Python environment lets you run them against real model input; subscriptions start at ₹299 and require no local setup.

Primary sources

Frequently asked questions

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

Slice notation s[::-1] is the fastest and most concise. It creates a reversed copy in a single expression using Python's extended slicing with step -1. This is the expected answer in most placement coding rounds.

How do you reverse a string in Python without slicing?

Use reversed() with join(): ''.join(reversed(s)). Alternatively, build a loop that starts from the last index and appends characters to an empty string. Both produce the same O(n) result.

What is the difference between reversing a string and reversing each word?

Reversing a string flips all characters: 'Python' becomes 'nohtyP'. Reversing each word flips the characters inside every word but keeps word order: 'Python program' becomes 'nohtyP margorp'. Both are tested in placement rounds.

Can you reverse a string in Python using recursion?

Yes. Define reverse(s) to return s when len(s) is 1 or less, otherwise return reverse(s[1:]) + s[0]. This is O(n) time but uses O(n) call-stack space. Interviewers ask it to test recursive thinking; for production code, use slicing.

Do placement tests allow slicing to reverse strings in Python?

Yes. TCS NQT, AMCAT, and CoCubes all allow standard Python syntax including slicing. The interviewer may then ask you to implement a loop-based alternative to test your understanding of the underlying mechanics.

Why does Python string reversal always create a new string?

Python strings are immutable and their characters cannot be changed after creation. Every reversal method (slicing, reversed(), or loop) creates a new string object rather than modifying the original.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF