Hello Friends,Hello Developer,
This tutorial will provide an example of python get first and last digit of a number. you will learn first and last digits python example. we will help you to give an example of python program to find first and last digit of a number. In this article, we will implement a program to find first and last digit of a number python. you will do the following things for how to find first and last digit of a number in python.
There are several ways to get the first and 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.
number1 = 43567
# Get First Digit from Number
lastDigit = str(number1)
lastDigit = int(lastDigit)
firstDigit = str(number1)[-1]
firstDigit = int(firstDigit)
You can get the first and last digits of a number in Python using simple mathematical operations. Here's an example code:
In this example, we first convert the number to a string using str(num), then extract the first character of the string using indexing , and finally convert it back to an integer using int(). This gives us the first digit of the number.
To get the last digit, we use the modulus operator % to get the remainder of the division of num by 10, which is the last digit of num.
num = 12345
# Get the first digit
firstDigit = int(str(num))
# Get the last digit
lastDigit = num % 10
I hope it can help you...
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example