How to Get the Last Digit of a Number in Python?
Hi Dev,
This detailed guide will cover python get last digit of a number. In this article, we will implement a how to get last digit of a number in python. This example will help you program to find last digit of a number python. It's a simple example of last digit of a number in python. Let's see below example find last digit of a number in python.
There are several ways to get the last digit of a number in Python. I will give you two simple examples to get the last digit from a number. You can see both examples one by one.
Example 1:
main.py
number1 = 43567 # Get Last Digit from Number lastDigit = str(number1)[-1] lastDigit = int(lastDigit) print(lastDigit)
Output:
7
Example 2:
You can get the last digit of a number in Python using the modulus operator (%). Here's an example code:
In this example, the modulus operator % returns the remainder of the division of num by 10, which is the last digit of num.
main.py
number = 9658 # Get last Digit from Number lastDigit = num % 10 print(lastDigit)
Output:
8
I hope it can help you...