Python: Reverse a String

Problem Statement:

Write a Python function that takes a string as input and returns the reversed version of the string.

Solution Explaination:

  • In Python, we can use string slicing to reverse a string.
  • [::-1] means:
    • Start from the end of the string (-1 index).
    • Move backward until the beginning

Solution:

def reverse_string(s):
    return s[::-1]

print(reverse_string(“hello”))  # Output: “olleh”

Example Output:
Input: “hello”
Output: “olleh”

Leave a Reply