Python program: Reverse a given string
Python program: Reverse a given string
In this article, we will discuss the concept of the Python program: Reverse a given string
In this post, we are going to learn how to make a reverse string in Python programming language
Program to make the reverse of a string using for loop
The program allows to enter a String and it makes and displays the reversed String using for loop in Python language
Program 1
#Python program to reverse a given String str=input("Please enter a String: ") s="" for ch in str:#use for loop for reversed a string s=ch+s print("The reversed string is: ",s)
When te above code is executed, it produces the following result
Please enter a String: Python the reversed string is: nohtyP
Program to make the reverse of a string using while loop
The program allows to enter a String and it makes and displays the reversed String using while loop in Python language
Program 2
#Python program to reverse string using while loop string=input("Please enter a string: ") string1=' ' i=len(string)-1 while(i>=0): string1=string1+string[i] i=i-1 print("\nGiven String: ",string) print("\nReversed String: ",string1)
When te above code is executed, it produces the following result
Please enter a string: My String Given String: My String Reversed String: gnirtS yM
Program to Create reverse of a string using a user-defined function
Program 3
The program allows to enter a String and it makes and displays the reversed String using the function in Python language
#Python program to reverse a given String #using a user-defined function mystr=input("Please enter a String: ") def reverse(str): s="" for ch in str:#use for loop for reversed a string s=ch+s return s #reversed string print("The reversed string is: ",reverse(mystr))#call the function
When te above code is executed, it produces the following result
Please enter a String: Python language the reversed string is: egaugnal nohtyP
Suggested for you
Similar post
Java program to reversed a string using loops
C program to reverse a string using loops
C++ program to reverse a string using loops
Java program to reverse a given number using loops
C program to reverse a number using loops
C++ program to reverse a number using loops