Problem No 1
Reverse a String with Slicing and Reversed
Easy≈ 6 minute session
Lesson guide
What this Python exercise practices
Reverse a String with Slicing and Reversed is a beginner practice lesson that focuses on strings, formatting, traversal. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- String values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Related public exercises
Summary
Return a new string which is the reverse of the input string.
Problem statement
Write a function reverse_string(s) that takes a string s and returns a new string which is s reversed. The function should preserve all characters (including spaces, punctuation, and unicode) and only change their order.
Task
Practice basic string indexing and slicing by implementing a function that returns the reversed string.
Examples
Basic example
Input
reverse_string('hello')
Output
olleh
Explanation
The function returns the characters in reverse order.
Input format
A single string s passed as the argument to reverse_string(s).
Output format
Return a string that is the reverse of s.
Constraints
0 <= len(s) <= 10^5. The function should work with any unicode characters.
Samples
Sample input 0
reverse_string('abc def')
Sample output 0
fed cba
Explanation 0
Spaces are preserved and characters reversed.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.