Reverse a String

Theory:

To reverse a string, we iterate through the string from the end to the beginning and append each character to a new string.

Python Code:

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

# Taking input from the user
input_string = input("Enter a string to reverse: ")
print("Reversed string:", reverse_string(input_string))

Example:

Enter a string to reverse: Hello World

Reversed string: dlroW olleH

Code Explanation:

The function reverse_string(s) takes a string s as input and returns the reversed string using Python's slicing feature s[::-1].

The example usage section demonstrates how to take input from the user, reverse the string, and print the result.