Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Program To Reverse a Number

2 min read

Problem Definition

Create a Python program to reverse a number in Python.

Solution

This article will show multiple solutions for reversing a number in Python.

Reversing a number mathematically

The algorithm below is used to reverse a number mathematically with time complexity of O(log n) where n is input number.

Algorithm

Input : num

(1) Initialize rev_num = 0

(2) Loop while num > 0

  • (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num
  • (b) Divide num by 10

(3) Return rev_num

 Program

num = 12345
rev_num = 0
while num != 0:
    rev_num = rev_num * 10
    rev_num = rev_num + (num%10)
    num = num // 10

print(rev_num)

Output

54321

Using reversed() method

Python's built in reversed() method returns an iterator that accesses the given sequence in the reverse order.

Program

# input
num = 1234

rev_iterator = reversed(str(num))

rev_num = "".join(rev_iterator)
print(rev_num)

Output

4321

Note that the reversed() method doesn't accept integer as a parameter therefore data type is converted to a string.

Since reversed() returns an iterator we need to join it before printing it out.

Using slicing

Python string supports slicing to create substring.

str_object[start_pos:end_pos:step]

The slicing starts with the start_pos index (included) and ends at the end_pos index (excluded). The step parameter is used to specify the steps to take from the start to end index.

Program

num = 123

rev_num = str(num)[::-1]
print(rev_num)

Output

321

Since slicing work on a string we need to convert the number to a string, then create a slice that starts with the length of the string, and ends at index 0 with a step of -1.

Suggested read:


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read