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
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
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 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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…