Get the last 2 digits of a Number in Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Get the last 2 digits of a Number in Python #

Use the modulo operator to get the last 2 digits of a number, e.g. last_two = number % 100. The modulo operator will return the last 2 digits of the number by calculating the remainder of dividing the number by 100.

main.py
number = 123456 last_two = number % 100 print(last_two) # 👉️ 56

If the number might be negative, use the abs() function to make sure you get the correct result.

main.py
number = -123456 last_two = abs(number) % 100 print(last_two) # 👉️ 56

The abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned, and if the number is negative, the negation of the number is returned.

main.py
print(abs(-43)) # 👉️ 43 print(abs(43)) # 👉️ 43

The modulo (%) operator returns the remainder from the division of the first value by the second.

main.py
print(10 % 2) # 👉️ 0 print(10 % 4) # 👉️ 2

If the value on the right-hand side is zero, the operator raises a ZeroDivisionError exception.

We used integers in the example, but the left and right-hand side values may also be floating point numbers.

The modulo % operator divides the number by 100 and returns the remainder.

For example, the remainder of dividing 123456 by 100 is 56.

main.py
number = 123456 print(123456 - 1234 * 100) # 👉️ 56

Here are some more examples.

main.py
print(23484 % 100) # 👉️ 84 print(9590 % 100) # 👉️ 90 print(900 % 100) # 👉️ 0

Alternatively, you can use string slicing.

Get the last 2 digits of a Number in Python #

To get the last 2 digits of a number:

  1. Use the str() class to convert the number to a string.
  2. Use string slicing to get the last 2 characters of the string.
  3. Use the int() class to convert the result to an integer.
main.py
number = 123456 last_two = int(str(number)[-2:]) print(last_two) # 👉️ 56

We used the str() class to convert the integer to a string so we can use string slicing.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

Negative indices can be used to count backwards.

The slice my_str[-2:] starts at the second to last character and goes to the end of the string.

Once we have the last 2 digits, we can use the int() class to convert the string to an integer.

main.py
number = 123456 last_two = int(str(number)[-2:]) print(last_two) # 👉️ 56

Which approach you pick is a matter of personal preference. I'd go with using the modulo % operator because it's quite intuitive and easy to read.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.