How to Get Last 2 Digits of a Number in Python?

By Hardik Savani October 30, 2023 Category : Python

Hi Folks,

Today, I would like to show you how to get last 2 digits of a number in python. you will learn python get last 2 digits of number. This article will give you a simple example of get last 2 digits of number python. This post will give you a simple example of how to get last 2 digits of a number in python. you will do the following things for how to get last 2 digits of a number in python.

To get the last 2 digits of a number in Python, you can use the modulo operator %. Here's an example code:

In this code, we assign the example number 123456 to the variable num. We then use the modulo operator % to get the remainder of num divided by 100, which is the same as dividing by 10^2 (the number of digits we want to extract). This gives us the last two digits of the number. We store the result in the variable lastTwoDigits and print it out.

Example 1:

main.py

num = 123456
  
# Get Last 2 Digits from Number
lastTwoDigits = num % 100 
  
print(lastTwoDigits)

Output:

56

I hope it can help you...

Tags :
Shares