15-Jun-2023

.

Admin

Hi Dev,

Now, let's see the post of get the first digit of the number in the Python tutorial example. This article goes in detailed on Python getting the first digit of a number. I explained simply how to get the first digit of a number in Python. We will look at an example of a program to find the first digit of a number Python. Here, Creating a basic example of the first digit of a number in Python.

There are several ways to get the first digit of a number in Python. I will give you two simple examples to get the first digit from a number. You can see both examples one by one.

**Example 1:**

**main.py**

number1 = 73567

# Get First Digit from Number

firstDigit = str(number1)[0]

result = int(firstDigit)

print(result)

**Output:**

7

**Example 2:**

You can use the modulus operator (%) and division operator (/) in Python to get the first digit of a number.

In this code, we first calculate the length of the number using len(str(number)). Then, we calculate the power of 10 by raising it to the length of the number minus 1. Next, we divide the number by the power of 10, which gives us a number with only the first digit. Finally, we use integer division (//) to get rid of any decimal places and assign the result to the firstDigit variable.

**main.py**

number =3658

# Get First Digit from Number

firstDigit = number // (10 ** (len(str(number))-1))

print(firstDigit)

**Output:**

3

#Python

- How to Replace Special Characters with Space in Python String?
- Laravel str pluralStudly() function Example
- Install Lamp, PHPmyadmin on Ubuntu 18.04
- Laravel Get Current Timestamp Example
- Split String into List of Characters in Python
- Laravel 9 Charts JS Chart Example Tutorial
- Laravel 9 Add Image to PDF File Example
- How to Apexcharts using Larapex Charts in Laravel 11?
- JQuery Get and Set Image Src Example Tutorial
- How to Find First Occurrence of Array in Javascript?