Get the First Digit of a Number in Python

I was working on a project where I needed to extract the first digit of a number in Python. At first, it sounds simple, but depending on the approach you use, there are multiple ways to solve this.

As someone who has been writing Python code for more than 10 years, I’ve faced this requirement many times. Whether I was building a data-cleaning script for financial data or parsing numeric IDs in a U.S.-based customer database, quickly obtaining the first digit became essential.

In this tutorial, I’ll show you four easy methods to get the first digit of a number in Python. I’ll also share my firsthand experience with each method so you can decide which one is best for your project.

Method 1 – Convert Number to String and Slice

The easiest way to get the first digit is to convert the number into a Python string and then take the first character.

Here’s how I do it:

# Method 1: Using string slicing
num = 98765

# Convert to string and get the first character
first_digit = int(str(num)[0])

print("The first digit is:", first_digit)

Output:

The first digit is: 9

I executed the above example code and added the screenshot below.

how to get first digit of a number in python

This method is super quick and works well when you’re dealing with integers. I often use it when parsing U.S. ZIP codes stored as integers in a dataset.

Method 2 – Use a While Loop

Another method is to repeatedly divide the number by 10 until only one digit remains.

Here’s the code:

# Method 2: Using while loop
num = 98765

while num >= 10:
    num //= 10   # Integer division

print("The first digit is:", num)

Output:

The first digit is: 9

I executed the above example code and added the screenshot below.

how to get the first digit of a number in python

This approach feels more mathematical. I usually prefer this when I’m working with numeric data directly, without converting to strings.

Method 3 – Use Logarithms (math module)

If you want a more mathematical solution, you can use the math.log10() function in Python.

import math

# Method 3: Using math.log10
num = 98765

# Find the number of digits
digits = int(math.log10(num))

# Divide to get the first digit
first_digit = num // (10 ** digits)

print("The first digit is:", first_digit)

Output:

The first digit is: 9

I executed the above example code and added the screenshot below.

get first digit of int python

This method is efficient for very large numbers, especially when working with datasets like population counts or transaction IDs.

Method 4 – Use Recursion

Sometimes, I like to solve problems recursively. Here’s how you can do it:

# Method 4: Using recursion
def get_first_digit(num):
    if num < 10:
        return num
    return get_first_digit(num // 10)

num = 98765
print("The first digit is:", get_first_digit(num))

Output:

The first digit is: 9

This method isn’t always the most efficient, but it’s a fun way to practice recursion.

Handle Negative Numbers

All the above methods work with positive integers. But what if your number is negative?

Here’s a quick fix:

num = -98765

# Convert to positive before applying methods
first_digit = int(str(abs(num))[0])

print("The first digit is:", first_digit)

Output:

The first digit is: 9

I often encounter this when working with financial data in the U.S., where negative numbers represent expenses or losses.

Handle Floating-Point Numbers

If you’re working with decimal numbers, you can still extract the first digit.

num = 123.456

first_digit = int(str(num)[0])

print("The first digit is:", first_digit)

Output:

The first digit is: 1

For floats, the string method is usually the simplest.

Which Method Should You Use?

  • String slicing → Best for quick and easy solutions.
  • While loop → Best when working with integers in pure math operations.
  • Logarithms → Best for very large numbers.
  • Recursion → Best for learning or practicing recursion.

In my projects, I usually go with string slicing unless performance is critical.

Obtaining the first digit of a number in Python is simple once you are familiar with the various approaches. Each method has its own advantages, and the one you choose depends on your use case.

Other Python tutorials you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.