String

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

Reversed string

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

for loop in Python

while loop in Python

Function in Python

 

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

 

Python Program for count upper or lowercase letter of the given string
C program to Count the total number of characters in the given string including space
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago