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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Remove All Decimals from Number Example
- Python Convert String to Float with 2 Decimal Places Example
- How to Sort List Descending in Python?
- How to Create an Alphabet List in Python?
- Python Search String in File and Print Line Example
- How to Check If Element is Present in List Python?
- How to Modify JSON File in Python?
- How to Check If a List is Empty or Not in Python?
- Python String Convert to Lowercase Example
- How to Get Min Value from Python List?
- How to Remove null Values from the List in Python?
- How to Remove Last Element from List in Python?
- Python Post Request with Json File Example
- Python Copy File From one Directory to Another Example