Python program to find reverse of a number using slice

Hello every one, welcome back ! Here we will learn a simple python logic to reverse a number.

Here we read a number from user and reverse that number using slice operations. We first cast that integer number as string, and then perform a slicing operation to reverse the number.

Syntax : str(num)[::-1])

We can also reverse a number without using slice operations, that is discussed in another post.

Program on Github

Reverse a number – Code Visualization

Task :

To reverse a given integer number.

Approach :

  • Read an input number using input() or raw_input().
  • Check whether the value entered is integer or not.
  • We convert that integer number to string str(num).
  • Now we use advanced slice operation [start:end:step] leaving start and empty and giving step a value of -1, this slice operation reverses the string.
  • Print the result.

Program on Github

Program :

Output :

Reverse an integer number - programminginpython.com
Reverse an integer number – programminginpython.com
Reverse an integer number - programminginpython.com
Reverse an integer number – programminginpython.com
Reverse an integer number - programminginpython.com
Reverse an integer number – programminginpython.com

Program on Github

One Comment

  1. Pingback: Python program to find reverse of a number - Programming in Python

Leave a Reply