# Python Get First and Last Digit of a Number Example

Hello Friends,

Here, I will show you python get first and last digit of a number. step by step explain first and last digits python example. We will look at an example of python program to find first and last digit of a number. This tutorial will give you a simple example of program to find first and last digit of a number python.

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

**Example 1:**

main.py

number1 = 43567 # Get First Digit from Number lastDigit = str(number1)[0] lastDigit = int(lastDigit) firstDigit = str(number1)[-1] firstDigit = int(firstDigit) print(firstDigit) print(lastDigit)

Output:

4 7

**Example 2:**

You can get the first and last digits of a number in Python using simple mathematical operations. Here's an example code:

In this example, we first convert the number to a string using str(num), then extract the first character of the string using indexing [0], and finally convert it back to an integer using int(). This gives us the first digit of the number.

To get the last digit, we use the modulus operator % to get the remainder of the division of num by 10, which is the last digit of num.

main.py

num = 12345 # Get the first digit firstDigit = int(str(num)[0]) print(firstDigit) # Get the last digit lastDigit = num % 10 print(lastDigit)

Output:

1 5

I hope it can help you...

### Hardik Savani

I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.

#### We are Recommending you

- How to Get the First Digit of a Number in Python?
- How to Create an Alphabet List in Python?
- Python Generate List of Random Strings Example
- Python Generate List of Random Float Numbers Example
- Python String Replace Double Quotes with Single Quotes Example
- How to Remove All Numbers from a String in Python?
- How to Remove All Spaces from a String in Python?
- Python Replace Last Character Occurrence in String Example
- Python Count Number of Special Characters in String Example
- Python Count Number of Specific Characters in String Example
- Python Count Number of Characters in String Example
- How to Get First and Last Character of String in Python?
- Python Convert List into String with Commas Example
- Python Delete Directory if Exists Example