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:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers