Python Program to Reverse a String

« Previous Program Next Program »

Reverse a String in Python

To reverse a string entered by the user in python, you have to ask from user to enter the desired string which is going to reverse to reverse that string and print the reversed string as output as shown in the program given below.

Python Programming Code to Reverse a String

Following python program ask from the user to enter a string to reverse that string and print it as output:

# Python Program - Reverse String

print("Enter 'x' for exit.");
string = input("Enter any string to reverse it: ")
if string == 'x':
    revstring = string[::-1];
    print("\nOriginal String =",string);
    print("Reversed String =",revstring);

Here is the sample run of the above python program to illustrate how to reverse a string in python:

reverse string program python

Now enter any string say codescracker to reverse it and print it out on output screen which will be rekcarcsedoc:

reverse string python

Same program on python shell:

python reverse a string

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:

« Previous Program Next Program »