Write A Python Function To Reverse The Given String

Given a string as input to the Python program your task is to reverse the string and print it.  use the function to reverse a string by taking the argument as an original string.


There are two ways to perform   

    First using the following
    Second using the string slicing technique

 

Write A Python Function To Reverse The Given String

 





Write A Python Function To Reverse The Given String using String Slicing

Define a function name as string_reverse() and pass the argument to the function as input string.  used in slicing technique [::-1]. This will reverse the given string and using the return statement return the reverse of a string.

def reverse_string(text):
   return text[::-1]





In the main program take a  string variable,  ask a user to enter the string using the input function call.  then take another string variable and all the functions to reverse the string and pass the user entered  input string as an argument.  but the function is executed print the reverse string using print function call


my_string = input("Enter a string : ")
output = reverse_string(my_string)
print("Reversed String is ...")
print(output)




Python code using the string slicing method
===============================================

def reverse_string(text):
   return text[::-1]


my_string = input("Enter a string : ")
output = reverse_string(my_string)
print("Reversed String is ...")
print(output)





Write A Python Function To Reverse The Given String using for loop

Let's define a function named reverse_string() and pass the input as a string to the function,  take a variable named data and set it to an Empty string using the for loop to iterate all the letters of an input string, add each letter in the front of the data variable. Return the data variable.

def reverse_string(text):
   data = ""
   for letter in text:
       data = letter + data
   return data




In the main program take a  string variable and ask the user to enter the string using the input function call,  call the function to reverse a string and print the return value of the function in the main program.


my_string = input("Enter a string : ")
output = reverse_string(my_string)
print("Reversed String is ...")
print(output)




Code
=============================

def reverse_string(text):
   data = ""
   for letter in text:
       data = letter + data
   return data


my_string = input("Enter a string : ")
output = reverse_string(my_string)
print("Reversed String is ...")
print(output)





Output
==================

Enter a string : how are you ?
Reversed String is ...
 ? uoy era woh



Output
==================

Enter a string : you there
Reversed String is ...
 ereht uoy




Output
============

Enter a string : reverse a string
Reversed String is ...
 gnirts a esrever




Conclusion
==================

Execute the above program by providing the random input strings.

Comment it down below if you have any suggestions or queries.





Post a Comment

0 Comments