# How to Get the first digit of a Number in Python [4 Methods]

There are many ways we can get the first digit of a number in Python.

In this Python article, we will cover several methods to get the first digit of a number in Python, such as iterating over the number using a while loop or searching by identifying the position through indexing or string slicing and by recursion.

Let’s see them all with examples in detail:

## Python program to get the first digit of a number using a while loop

We will take a number in Python while declaring the variable. Then, we will run the while loop to find the first digit of the given number and divide it by 10 until the number is greater than 10. Finally, it displays the first digit of the number.

This is the source code of Python to get the first digit of a number using a while loop.

``````Price = int(input('Enter any Number: '))

while (Price>= 10):
Price = Price // 10

print('The first digit of the given price is:', Price)``````

Output:

``````Enter the price of product: 1245
The first digit of the given price is: 1``````

You can refer to the screenshot below to see the output of the above Python code to get the first digit of the number using a while loop.

## How to get the first digit of the number in Python using indexing

The easiest way to get the first digit of a number is to convert it to a string and access the first element. String provides indexing or an [ ] bracket to access any character in the string by index position.

As indexing of characters in a string starts from 0, So to get the first character pass the index position 0 in the [ ] bracket.

Here is the complete Python program to get the first digit of the number in Python using indexing.

``````population = int(input('Enter the number: '))

population = str(population)

first_digit = population[0]
print('The first digit of number:', first_digit)``````

Output:

``````Enter the number: 29559788
The first digit of number: 2``````

Refer to the image below, and it will display the output of the Python program to get the first digit of the number using indexing.

## Get the first digit of the number in Python using string slicing

In Python, string slicing is a feature that allows you to access and manipulate specific parts of a string by slicing it respectively from start to end.

This is the full program of how to get the first digit of the number in Python using string slicing.

`````` Alska_Drivers_License = int(input('Enter the DL number: '))

print("The first digit of the Alska driver's license number:", first_digit)``````

I have taken [:1] to specify the character at index 0. The string [:1] specifies the first characters of the given string.

``first_digit = Alska_Drivers_License[0:1]``

Output:

``````Enter the DL number: 6378123
The first digit of the Alska driver's license number: 6``````

The screenshot below displays the output of the Python program to get the first digit of the number using the string slicing.

## How to get the first digit of a number in Python using recursion

Recursion is a method in Python that is used to break a problem into smaller and smaller sub-problems until we get to a small enough problem that can be solved.

READ:  How to Split the Last Element of a String in Python

In simple words, a process in which a function calls itself directly or indirectly in Python is called recursion.

This is the complete Python code to get the first digit of a number in Python using recursion.

``````def get_first_digit(ID):
if ID < 10:
return ID
return get_first_digit(ID // 10)

print(get_first_digit(246811))
print(get_first_digit(51457873))
print(get_first_digit(1469334))
print(get_first_digit(8111248))
print(get_first_digit(1828341))``````

Here, we check if the given ID is less than 10. If it is True, we return the value of the ID. Then, it recursively calls the get_first_digit function with ID divided by 10. This operation removes the last digit of the ID and continues to check for the next digit until it finds the first digit.

Output:

``````2
5
1
8
1``````

You can refer to the screenshot below to see the output of the Python program to get the first digit of the number in Python using recursion.

## Conclusion

I hope the information provided in the above Python post gives a clearer picture of how to get the first digit of a number in Python.

I have explained various methods with an illustrative example, such as using a while loop, indexing, slicing, and recursion methods for getting the first digit of a number.

You may also like to read the following tutorials: