Get Last 2 Digits of a Number in Python

AuthorSumit Dey Sarkar

Pubish Date11 Apr 2023

categoryPython

In this tutorial we will learn how to Get Last 2 Digits of a Number in Python.

 

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 (%) to extract the remainder when the number is divided by 100.

Here's an example:

 

num = 123456
last_two_digits = num % 100
print(last_two_digits) # Output: 56

In this example, the variable num is assigned the value of 123456. We then use the modulo operator (%) to extract the remainder when num is divided by 100. This gives us the last two digits of the number, which are assigned to the variable last_two_digits. Finally, we print out the value of last_two_digits, which is 56.

You can replace the value of num with any integer you want to extract the last two digits from.

Comments 0

Leave a comment