Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Reverse a String with Slicing and ReversedE02. Check if a string is a palindromeE03. Count Vowels Using String MethodsE

Problem No 1

Reverse a String with Slicing and Reversed

Easy

6 minute session

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.

Code editor
Loading editor…

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.

09:20 AM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.