Python Program that Reverses a Given String

Write a Python  Program to Reverse a string

Python Examples-InterviewGIGHere’s a Python program that reverses a given string:

string = input("Enter a string: ")
reversed_string = string[::-1]
print("Reversed string:", reversed_string)

Explanation:

  • The program starts by taking a string input from the user using the input() function and storing it in the variable string.
  • The syntax string[::-1] is used to slice the string with a step of -1, effectively reversing the string. This creates a reversed version of the input string, which is stored in the variable reversed_string.
  • The print() function is used to display the reversed string using the reversed_string variable.

Output:

Enter a string: Hello, World!
Reversed string: !dlroW ,olleH

In this example, if the user enters the string “Hello, World!”, the program uses string slicing to reverse the string and displays the reversed version as output: “!dlroW ,olleH”.

Scroll to Top