Get the last or last N digits of a Number in Python

Borislav Hadzhiev

Last updated: Feb 21, 2023

Reading timeยท5 min

- Get the last digit of a number in Python
- Get the last digit of a number using the modulo operator
- Get the last N digits of a Number in Python
- Get the last N digits of a Number using string slicing

**To get the last digit of a number:**

- Use the
`str()`

class to convert the number to a string. - Access the character at index
`-1`

. - Use the
`int()`

class to convert the result to an integer.

main.py

`number = 24685 # โ get the last digit of a number (using str()) last_digit = int(str(number)[-1]) print(last_digit) # ๐๏ธ 5`

If you need to

get the last N digits of a number, click on the following subheading:

We used the str() class to convert the integer to a string so we can access the string at a specific index.

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`

.The next step is to access the character at index `-1`

to get the last digit of
the number.

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

main.py

`number = 24685 last_digit = int(str(number)[-1]) print(last_digit) # ๐๏ธ 5`

You can use the same approach if you need to get the last N digits of a number.

main.py

`number = 24685 last_two = int(str(number)[-2:]) print(last_two) # ๐๏ธ 85 last_three = int(str(number)[-3:]) print(last_three) # ๐๏ธ 685`

Negative indices can be used to count backward.

Alternatively, you can use the modulo operator.

main.py

`number = 24685 last_digit = number % 10 print(last_digit) # ๐๏ธ 5`

The modulo operator will return the last digit of the number by calculating the remainder of dividing the number by 10.

If the number might be negative, use the `abs()`

function to make sure you get
the correct result.

main.py

`number = -24685 last_digit = abs(number) % 10 print(last_digit) # ๐๏ธ 5`

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(-27)) # ๐๏ธ 27 print(abs(27)) # ๐๏ธ 27`

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 `10`

and returns the remainder.For example, the remainder of dividing `12345`

by `10`

is `5`

.

main.py

`number = 12345 print(12345 - 1234 * 10) # ๐๏ธ 5`

Here are some more examples.

main.py

`number = 12345 print(82342 % 10) # ๐๏ธ 2 print(248 % 10) # ๐๏ธ 8 print(150 % 10) # ๐๏ธ 0`

You can also use the modulo operator to get the last N digits of a number.

The modulo operator in the example 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 last_three = number % 1000 print(last_three) # ๐๏ธ 456`

The same approach can be used to get the last 3 digits of a number.

main.py

`number = 246810 # โ Get the last 3 digits of a number 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 = -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

`# โ Get the last 2 digits of a number print(23484 % 100) # ๐๏ธ 84 print(9590 % 100) # ๐๏ธ 90 print(900 % 100) # ๐๏ธ 0`

And here is an example of getting the last three digits of a number.

The remainder of dividing `123456`

by `1000`

is `456`

.

main.py

`print(123456 - 123*1000) # ๐๏ธ 456`

Here are some more examples.

main.py

`# โ Get the last 3 digits of a number print(123456 % 1000) # ๐๏ธ 456 print(8523400 % 1000) # ๐๏ธ 400 print(1523000 % 1000) # ๐๏ธ 0`

Alternatively, you can use string slicing.

This is a three-step process:

- Use the
`str()`

class to convert the number to a string. - Use string slicing to get the last N characters of the string.
- Use the
`int()`

class to convert the result to an integer.

main.py

`number = 123456 # โ Get the last 2 digits of a number last_two = int(str(number)[-2:]) print(last_two) # ๐๏ธ 56`

The same approach can be used to get the last three digits of a number.

main.py

`number = 246810 # โ Get the last 3 digits of a number 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 backward.

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.

You can learn more about the related topics by checking out the following tutorials: