Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Reverse String

Reverse character order with a Python slice. Students read the negative step in [::-1] and distinguish reversing text positions from reversing whole-word order.

BeginnerPhase 5 - Category 1: Basic String HandlingExample 2 of 10
reverse-string.py
1# Take string input
2s = input("Enter a string: ")
3
4# Reverse string
5reversed_str = s[::-1]
6print(f"Reversed: {reversed_str}")

Output

Enter a string: Hello
Reversed: olleH

In this one

  • Read a negative slice step
  • Reverse characters
  • Preserve the original string

How it works

  1. 1

    Read the entered text.

  2. 2

    Create reversed_str using s[::-1].

  3. 3

    Print the new reversed text.

Edge cases

What happens to an empty string?

What happens to an empty string?

It stays empty.

Does this reverse only word order?

Does this reverse only word order?

No. Characters inside words are reversed too.

What's going on

s[::-1] uses a step of negative one with omitted bounds. It produces a new string by reading positions backward.

For red blue, the result is eulb der. Both letters and spaces change positions. The source string is unchanged because strings are immutable. This reverses Unicode code points, which may split a multi-code-point visible symbol rather than treating it as one unit.

What happens to an empty string? It stays empty.

Does this reverse only word order? No. Characters inside words are reversed too.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the slice step?

Pick an answer to check it.

Question 2

What happens to an empty string?

Pick an answer to check it.

Questions people ask

What happens to an empty string?

It stays empty.

Does this reverse only word order?

No. Characters inside words are reversed too.

What is the slice step?

-1.