Editor

Reverse a String

Strings · Beginner · 40 XP · about 3 min

Write a function called reverse_string(s) that returns the reversed version of the input string.

Example

reverse_string("hello")   # Returns "olleh"
reverse_string("Python")  # Returns "nohtyP"
reverse_string("a")       # Returns "a"
reverse_string("")        # Returns ""

Requirements

  • Return the reversed string
  • Handle empty strings
  • Handle single character strings
def reverse_string(s):
# Your code here
pass
# Test your function
print(reverse_string("hello"))
print(reverse_string("Python"))

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.