In Python, there are a number of ways we can get the first digit of a number. The easiest way to get the first digit of a number is to convert it to a string and access the first element.
def getFirstDigit(num):
return str(num)[0]
print(getFirstDigit(100))
print(getFirstDigit(213))
#Output:
1
2
The second way is we can define a loop to continue dividing by 10 until get to a number between 0 and 10 to find the first digit.
def getFirstDigit(num):
while num >= 10:
num = int(num/10)
return num
print(getFirstDigit(100))
print(getFirstDigit(213))
#Output:
1
2
Finally, we can use the Python math module and get the help of the log10() function to get the first digit of a number.
import math
def getFirstDigit(num):
return int(num / 10 ** int(math.log10(num)))
print(getFirstDigit(100))
print(getFirstDigit(213))
#Output:
1
2
When working with numbers in computer programming, it is useful to be able to easily extract information from our variables.
One such piece of information is the digits that a number is made up of. In Python, we can easily get the digits of a number.
In particular, using Python, we can easily obtain the first digit of a number.
The easiest way to get the first digit of a number in Python is converting the number to a string variable, and then access the first element of that string.
Below is a Python function for you to be able to extract the first digit of a number in your Python code.
def getFirstDigit(num):
return str(num)[0]
print(getFirstDigit(100))
print(getFirstDigit(213))
#Output:
1
2
Using a Loop to Get the First Digit of a Number in Python
There are a few other ways that we can find the first digit of a number in Python. A second method is to continue dividing by 10 until we get to a number between 0 and 10.
Doing this method in Python is easy with a while loop.
Below is an example of a function for how to get the first digit of a number using a loop in Python.
def getFirstDigit(num):
while num >= 10:
num = int(num/10)
return num
print(getFirstDigit(100))
print(getFirstDigit(213))
#Output:
1
2
Using the Python math Module to Get the First Digit of a Number in Python
One final method of how to obtain the first digit of a number in Python is to use the Python math module.
To get the first digit, we can use the Python math log10() function.
In the loop example, we divided by 10 until we got to a number between 0 and 10. By using the log10() function, we can find out exactly how many times we need to divide by 10 and then do the division directly.
Below is a sample program of how to get the first digit of a number using the math module in Python.
import math
def getFirstDigit(num):
return int(num / 10 ** int(math.log10(num)))
print(getFirstDigit(100))
print(getFirstDigit(213))
#Output:
1
2
Getting the Last Digit of a Number in Python
So far in this article, we’ve been talking about how to get the first digit of a number. We can also easily get the last digit of a number by modifying the first example slightly.
Instead of accessing the first element of the string variable, we access the last element of the string variable with slicing.
Below is how to get the last digit of a number using slicing in Python.
def getLastDigit(num):
return str(num)[-1:]
print(getLastDigit(100))
print(getLastDigit(213))
#Output:
0
3
Hopefully this article has been useful for you to learn 3 ways of how to get the first digit of a number in Python.