Recently, I was working on a project where I needed to extract the first and last digit of a number in Python.
At first, I thought Python might have a built-in function for this. But just like in Excel, where some features require workarounds, in Python, we need to use a few tricks.
In this tutorial, I’ll show you three simple methods I personally use to get the first and last digit of a number. These methods are easy to understand and work perfectly for day-to-day coding tasks.
Method 1: Use Python while loop
A while loop in Python is used to execute a set of statements until the condition is True or False.
MacbookM2_Price = int(input("Enter the price of Macbook M2 price: "))
first_digit = MacbookM2_Price
while first_digit >= 10:
first_digit //= 10
last_digit = MacbookM2_Price % 10
print("First digit:", first_digit)
print("Last digit:", last_digit)We use the modulo % operator in Python when the modulo (number) is divided by 10 and returns the last digit, and to find the first digit of a number, we divide the given number by 10 until the number is greater than 10.
first_digit //= 10
last_digit = MacbookM2_Price % 10 Output:
Enter the price of Macbook M2 price: 110892
First digit: 1
Last digit: 2You can see the output in the screenshot below.

This method uses a while loop and modulo division to extract the first and last digits of a number efficiently.
Method 2: Use indexing in Python
I will use the [] bracket to access any character in the string by index position in Python. We need to pass the index position in the square brackets, which will return the character at that index.
population = int(input('Enter the number: '))
population = str(population)
first_digit = population[0]
last_digit = population[-1]
print('The first digit of number:', first_digit)
print('The last digit of number:', last_digit)Output:
Enter the number: 98734567
The first digit of number: 9
The last digit of number: 7You can see the output in the screenshot below.

This method converts the number to a string and uses indexing to easily access the first and last digits.
Method 3: Use a Math Module with Log Function
Python provides the math module, the built-in module for mathematical tasks. The math module has a set of methods and constants.
import math
def firstDigit(temperature) :
digits = (int)(math.log10(temperature))
temperature = (int)(temperature / pow(10, digits))
return temperature;
def lastDigit(temperature) :
return (temperature % 10)
temperature = 29;
print(firstDigit(temperature), end = " " )
print(lastDigit(temperature))Here, I have used math.log10 from the math module in Python to calculate the number of digits using the logarithm function. The pow() function is used to calculate the power of 10 and returns the extracted first digit.
Then, by using the modulo operator %, we get the last digit of the temperature.
def firstDigit(temperature) :
digits = (int)(math.log10(temperature))
temperature = (int)(temperature / pow(10, digits))
return temperature;
def lastDigit(temperature) :
return (temperature % 10) Output:
2 9You can see the output in the screenshot below.

This method uses the math.log10() and pow() functions in Python to calculate the first digit and the modulo operator % to extract the last digit of a number.
I hope you find the Python article useful. Here, we have understood and learned how to get the first and last digits of a number in Python. Also, we have seen some ways to perform a Python program to get a number’s first and last digit from the given number.
Methods explained in this post to get the first and last digit of a number in Python, such as using a while loop, indexing, and the math module with a log function.
You may also like to read:
- Print an Array in Python
- Remove Elements from an Array in Python
- ValueError: Can Only Convert an Array of Size 1 to a Python Scalar
- Create an Array from 1 to N in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.