Given a number such as 34759, the task is to find its first digit (3) and last digit (9). This is a common beginner exercise in Python, and it can be solved in three ways: by converting the number to a string, with a loop, or with logarithms. This page shows all three, explains which one to use, and covers negative numbers and other tricky inputs.
Finding the Last Digit
The last digit of a number is the remainder when you divide it by 10:
number = 34759
last_digit = number % 10
print(last_digit) # 9
Method 1: Using a String (Simplest)
Convert the number to a string, then take the first character (s[0]) and the last character (s[-1]):
number = int(input("Enter a number: "))
s = str(abs(number)) # abs() removes a minus sign, if any
first_digit = int(s[0])
last_digit = int(s[-1])
print("First digit:", first_digit)
print("Last digit:", last_digit)
Output:
Enter a number: 34759
First digit: 3
Last digit: 9
Method 2: Using a Loop (Pure Maths)
Keep dividing the number by 10 with integer division (//) until only one digit is left. That digit is the first digit.
number = int(input("Enter a number: "))
n = abs(number)
last_digit = n % 10
first_digit = n
while first_digit >= 10:
first_digit //= 10
print("First digit:", first_digit)
print("Last digit:", last_digit)
Output:
Enter a number: 47982
First digit: 4
Last digit: 2
For 47982, the loop runs 47982 → 4798 → 479 → 47 → 4, then stops because 4 is less than 10.
Method 3: Using Logarithms (and Its Hidden Problem)
The number of digits in n is floor(log10(n)) + 1, so dividing n by 10 raised to floor(log10(n)) gives the first digit:
import math
number = 34759
first_digit = number // 10 ** math.floor(math.log10(number))
print(first_digit) # 3
This works for ordinary numbers, but it has two problems:
- It crashes for
0and negative numbers withValueError: math domain error. - For very large numbers, floating-point rounding can give a wrong answer. For example,
math.log10(999999999999999999)returns18.0instead of a value just below 18, so the formula gives0instead of9.
Because of this, prefer Method 1 or Method 2 in real programs.
Watch Out for Negative Numbers
In Python, % with a positive divisor always returns a non-negative result, so the last-digit trick gives a surprising answer for negative numbers:
print(-5821 % 10) # 9, not 1
print(abs(-5821) % 10) # 1
That is why both programs above use abs() first.
Test Your Program with These Inputs
| Input | First digit | Last digit |
|---|---|---|
| 34759 | 3 | 9 |
| 7 | 7 | 7 |
| 1000 | 1 | 0 |
| -5821 | 5 | 1 |
| 999999999999999999 | 9 | 9 |
Methods 1 and 2 give the correct answer for every row in this table.
Practice Variation: Sum of the First and Last Digit
number = 34759
s = str(abs(number))
print(int(s[0]) + int(s[-1])) # 3 + 9 = 12
Frequently Asked Questions
How do I get the last digit of a number in Python?
Use abs(number) % 10.
How do I get the first digit of a number in Python?
The simplest way is int(str(abs(number))[0]). Without strings, divide by 10 in a loop until the number is less than 10.
Which method is fastest?
For normal-sized numbers, all three are instant. The string method is the easiest to read, and the loop method is what teachers usually expect when they say “without using strings”.
Related: Writing your first Python program