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.
1# Take string input2s = input("Enter a string: ")34# Reverse string5reversed_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
Read the entered text.
- 2
Create reversed_str using s[::-1].
- 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.
๐Related Content
- ๐
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- ๐
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- ๐ป
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum