Get the last 3 digits of a Number in Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Get the last 3 digits of a Number in Python #

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

main.py
number = 246810 last_three = number % 1000 print(last_three) # 👉️ 810

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

main.py
number = -246810 last_three = abs(number) % 1000 print(last_three) # 👉️ 810

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(-50)) # 👉️ 50 print(abs(50)) # 👉️ 50

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 1000 and returns the remainder.

For example, the remainder of dividing 123456 by 1000 is 456.

main.py
print(123456 - 123*1000) # 👉️ 456

Here are some more examples.

main.py
print(123456 % 1000) # 👉️ 456 print(8523400 % 1000) # 👉️ 400 print(1523000 % 1000) # 👉️ 0

Alternatively, you can use string slicing.

Get the last 3 digits of a Number using string slicing #

To get the last 3 digits of a number:

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

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[-3:] starts at the third to last character and goes to the end of the string.

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

main.py
number = 246810 last_three = int(str(number)[-3:]) print(last_three) # 👉️ 810

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.