How to Get the First Digit of a Number in Python

Here, we will discuss how to get the first digit of a number in python. If the number was “12345” then print the first digit “1”. We will develop a Python program to get the first digit of the given number using native methods, built-in function, [ ] operator, and slice operator.

Python Program to Get First Digit of a Number

We will take a number while declaring the variable. Then, we will run the while loop to find the first digit of a number we divide the given number by 10 until the number is greater than 10. In the end, we are left with the first digit. Finally, the first digit of the number will be displayed on the screen.

# Python Program to get the first digit of number

# take input
num = int(input('Enter any Number: '))

# get the first digit
while (num >= 10):
    num = num // 10

# printing first digit of number
print('The first digit of number:', num)

Output:-

Enter any Number: 123
The first digit of number: 1

Get First Digit of Number using Built-in Functions

We are using the built-in functions called math.pow and log10. math.pow( ) method is a library method of the math module, it is used to calculate the given power of a base, it accepts two numbers and returns the first number to the power of the second number in the float. log10( ) is a mathematical function present in math.h header file. It returns the log base 10 value of the passed parameter to log10() function.

# Python Program to get the first digit of number

# importing math module
import math

# take input
num = int(input('Enter any Number: '))

# get the first digit
digits = int(math.log10(num))
first_digit = int(num / pow(10, digits))

# printing first digit of number
print('The first digit of number:', first_digit)

Output:-

Enter any Number: 523
The first digit of number: 5

How to Get First Digit of a Number in Python

In python, String provides an [ ] operator to access any character in the string by index position. We need to pass the index position in the square brackets, and it will return the character at that index. As indexing of characters in a string starts from 0, So to get the first character of the given string pass the index position 0 in the [ ] operator.

# Python Program to get the first digit of number

# take input
num = int(input('Enter any Number: '))

# convert int to string
num_str = str(num)

# get the first digit
first_digit = num_str[0]

# printing first digit of number
print('The first digit of number:', first_digit)

Output:-

Enter any Number: 7983516
The first digit of number: 7

Get First Digit of Number in Python using Slicing

We will get the first character of the string using the slice operator. The [:1] specifies the character at index 0. The string[:1] specifies the first characters of the given string.

# Python Program to get the first digit of number

# take input
num = int(input('Enter any Number: '))

# convert int to string
num_str = str(num)

# get the first digit
first_digit = num_str[:1]

# printing first digit of number
print('The first digit of number:', first_digit)

Output:-

Enter any Number: 100
The first digit of number: 1


Q) Write a program to print the digit at one’s place of a given number in Python.

num = 543169

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

print(num)

Output:- 5

Also See:- Reverse a Number in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Follow Us
Instagram • Twitter • Youtube • Linkedin • Facebook • Pinterest • Telegram

Leave a Comment

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