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

Hi, I’m Pramod Kumar Yadav & a computer science Engineer with 3years of experience in Digital marketing. I’m self-motivated and love to do SEO.




Best PYthon Course

Get More trending Courses

Leave a Reply

%d bloggers like this: