• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Get First Digit in Number Using Python

Get First Digit in Number Using Python

February 11, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Using Python to Remove First Character from String
  • 2.  How to Filter pandas DataFrame by Date
  • 3.  How to Declare Variable Without Value in Python
  • 4.  How to Group By Columns and Find Variance in pandas
  • 5.  pandas Standard Deviation – Using std() to Find Standard Deviation
  • 6.  Python math.factorial() function – Calculate Factorial of Number
  • 7.  Check if String is Date in Python
  • 8.  How to Check if Number is Divisible by 3 in Python
  • 9.  Using Python to Search File for String
  • 10.  Python Even or Odd – Check if Number is Even or Odd Using % Operator

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy