Python programming reverse number

How to reverse a number in Python

In this program, we will learn to reverse the number of digits. In python, there is a simple way to reverse the number by using the reverse() function. To reverse any number we have to pass the number inside the reverse function as argument.

Syntax:

reversed(variable)
 

Example-1: Source Code

#It will take user input value which will be consider as string
x = input('Enter your number or string to recerse: ')

r = list(reversed(x))

print("User input are:",x)

print(f"Reversed value of {'x'} :",r)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Enter your number or string to recerse: 12345
User input are: 12345
Reversed value of x : ['5', '4', '3', '2', '1']

Process finished with exit code 0

Example-2: Source Cod

You can also reverse the value in two and one lines of code in python.

example -1:

Reverse the value in two lines of code in Python

n='12345'
print(list(reversed(n)))
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['5', '4', '3', '2', '1']

Process finished with exit code 0

Example -2:

Reverse the value in one line of code in python

print(list(reversed('12345')))
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['5', '4', '3', '2', '1']

Process finished with exit code 0

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpurdham, Nepal. He was born on December 23, 1996, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India.He is director of Appan Bazaar Pvt.Ltd., as teacher in Tridev Eng. School , and an Educational Consultant, and he is currently working as a developer and Digital Marketer.



Leave a Comment