Reversing a string using python| How to reverse a string using python| Reversing a string using python using string traversing in python


Introduction

In this blog we are going to learn how to reverse a string and print the output using python.We will perform this operation using a basic approach ,that is ;without using predifined functions.

Understanding the code
  • First we will define a function using def() and name it as "reverse".
  • This function will have a parameter named "r".
  • the variable "r" will store the string ,entered by the user. 
  • At Last we will print the output using print() statement.  
Understanding the function defined:
  • First the length is compared and if the length is 0 then nothing will be printed.
  • If the else statement is executed then it will return the reversed string.
  • Here the string is reversed using a method called string traversing.
Souce Code :-
def reverse(r):
if len(r)==0:
return r
else:
return reverse(r[1:])+r[0]
r=str(input("Enter your string:"))
print(reverse(r))
#CODE ENDED

Output will look like:-


"Hope it helped"

Have anything in mind? Let me 
know in the comment section👇

 

Next Post Previous Post
No Comment
Add Comment
comment url