[ Web Proxy ]
URL:
Viewing: https://www.programminginpython.com/python-program-find-reverse-number-slice/#comments [Back]  [Original]

Python program to find reverse of a number using slice - Programming In Python
Skip to content
Home Python program to find reverse of a number using slice

Python program to find reverse of a number using slice

Python program to find reverse of a number using slice [Python program to find reverse of a number using slice] Python program to find reverse of a number using slice

Hello everyone, welcome back to Programming In Python! Here we will learn a simple Python logic to find the reverse of a number using a slice operation.

Here we read a number from the user and reverse that number using slice operations. We first cast that integer number as a 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, which is discussed in another post.

You can also watch the video on YouTube here.

Program on GitHub

Reverse a number Code Visualization

Task :

To reverse a given integer number.

Approach :

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

Program on GitHub

Program :

num = input('Enter any number : ')

try:
    val = int(num)
    print('Reverse of the given number is : ', str(num)[::-1])
except ValueError:
    print("That's not a valid number!")

 

Output :

Reverse of a number with slicing in Python [Reverse of a number with slicing in Python]The reverse of a number with slicing in Python

Reverse of a number with slicing in Python [Reverse of a number with slicing in Python]The reverse of a number with slicing in Python

Program on GitHub

You can also find the video on YouTube here.

Course Suggestion

Want to be strong at OOP in Python? If yes, I would suggest you take the course below.
Course:
Object Oriented Programming in Python

 

Online Python Compiler

Tagged: basic numbers reverse slicing

Post navigation

Leave a Reply Cancel reply

author:
email:
url:
comment:
ak_hp_textarea:

Programming In Python 2026. Powered By BlazeThemes.

Web Proxy Viewer  |  New URL  |  Original Page