In this article, we discuss will how to write a python program to find the reverse of string or number.
We will be discussing a total of 6 ways.
- Finding reverse of a number
- Using While loop
- Using recursion
- Finding reverse of a string
- By using slice
- Using inbuilt function
- Using iterative method
- And by using recursion
Let's discuss these ways one by one.
Find the Reverse of a Number
Using while loop
In this method, we will be using the while loop.
Code:-
# Taking a number from user
num = int(input("Enter a number: "))
# initializing variable rnum to 0. It is used to store the reverse of num
rnum = 0
# initializing temp variable to num
temp = num
# Reversing the num and storing in rnum variable using while loop
while temp > 0:
rnum = rnum*10 + (temp%10)
temp = temp//10
# printing the reverse of the number
print("The reverse of the number",num,"is",rnum)
Output:-
Enter a number: 58469
The reverse of the number 58469 is 96485
Using Recursion
In this, we will use the recursion. The recursive method reverseNumber() is used. In this program, we have also used the global keyword to use the global variable(temp) inside the function.
Code:-
# taking a temp variable to store the result from reverseNumber() function
temp = 0
# defining the reverseNumber() recursive function
def reverseNumber(num):
global temp
if num == 0:
return num
else:
temp = (temp * 10) + (num%10)
reverseNumber(num//10)
return temp
# Taking a number from user
num = int(input("Enter a number: "))
# initializing variable rnum to the return value of reverseNumber() function
rnum = reverseNumber(num)
# printing the reverse of the number
print("The reverse of the number",num,"is",rnum)
Output:-
Enter a number: 665948
The reverse of the number 665948 is 849566
Find the Reverse of a String
Using Slice
Here, we will use the slice technique to reverse the string and store it in the rs variable.
Code:-
# taking string
s= input("Enter a string: ")
# reversing the string using slice
rs = s[len(s)::-1]
# printing the reverse string
print("Reverse of string:",rs)
Output:-
Enter a string: spiderlabweb
Reverse of string: bewbalredips
Using inbuilt function
In this, we will be using the inbuilt functions like reversed() and join(). The reversed() function is going to return an object, so, join() function is used to convert that object to a string.
Code:-
# Taking a string from user
s = input("Enter a string: ")
# Reversing the string s using in-build function
rs = ''.join(reversed(s))
# printing the reverse string
print("Reverse of string:",rs)
Output:-
Enter a string: spiderlabweb
Reverse of string: bewbalredips
Using Iterative method
In this we will be using the for loop.
Code:-
# Taking a string from user
s = input("Enter a string: ")
# initializing variable rs to empty string(""). It is used to store the reverse of s
rs = ""
# checking for palindrome using the iterative method(for loop)
for i in s:
rs = i + rs
# printing the reverse string
print("Reverse of string:",rs)
Output:-
Enter a string: spiderlabweb
Reverse of string: bewbalredips
Using Recursion
In this, we will be using the recursion. I have defined a reverseString() recursive function.
Code:-
# defining the reverseString() recursive function
def reverseString(s):
if len(s) == 1:
return s
else:
return reverseString(s[1:]) + s[0]
# Taking a string from user
s = input("Enter a string: ")
# initializing variable rs to the return value from reverseString() function
rs = reverseString(s)
# printing the reverse string
print("Reverse of string:",rs)
Output:-
Enter a string: spiderlabweb
Reverse of string: bewbalredips
Hence we have discusses all the 6 ways to write this python program.
I have used these techniques to write python program for checking whether a number or string is palindrome or not.