Hi Friends,Hey Guys,
Now, let's see post of python get first digit of a number. It's a simple example of how to get first digit of a number in python. We will look at an example of program to find first digit of a number python. This example will help you first digit of a number in python. you will do the following things for find first digit of a number in python.
There are several ways to get the first digit of a number in Python. I will give you two simple examples to get the first digit from a number. You can see both examples one by one.
Example 1:
main.py
number1 = 43567
# Get First Digit from Number
firstDigit = str(number1)[0]
result = int(firstDigit)
print(result)
Output:
4
Example 2:
You can use the modulus operator (%) and division operator (/) in Python to get the first digit of a number.
In this code, we first calculate the length of the number using len(str(number)). Then, we calculate the power of 10 by raising it to the length of the number minus 1. Next, we divide the number by the power of 10, which gives us a number with only the first digit. Finally, we use integer division (//) to get rid of any decimal places and assign the result to the firstDigit variable.
main.py
number = 9658
# Get First Digit from Number
firstDigit = number // (10 ** (len(str(number))-1))
print(firstDigit)
Output:
9
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
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?