Python Program to Reverse a String

Strings are sequences of characters. They can be enclosed in quotation marks to make them appear as a single unit, and they can be accessed in several ways.

Program Code

# Get input string
input_str = input("Enter a string: ")

# Split the input string into a list of words
words = input_str.split()

# Reverse each word in the list
words = [word[::-1] for word in words]

# Join the list of words into a string
output_str = " ".join(words)

# Display the resultant string
print(output_str)

Input Given:

Enter a string: moc.reppePudE

Output Expected:

EduPepper.com

Code Explanation

  1. The input string is split into a list of words using the split() method.
  2. The list of words is iterated over using a for loop.
  3. Each word is reversed using the [::-1] slicing technique.
  4. The list of reversed words is joined into a string using the join() method.
  5. The resultant string is displayed.

Leave a Comment

Share to...