In Python, you can get the first digit of an integer in the following ways:

- Using Regular Expression;
- Using Floored Division;
- Reducing to Single Digit;
- Converting to String and Retrieving First Character.

## Using Regular Expression

You can use regular expression in the following way to get the first digit of an integer:

- Use the
`re.search()`

method with the`\d`

(or`[0-9]`

) pattern to get the first digit from numeric string; - Convert numeric string back to integer;
- Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

import re def first_digit(num): # 1: get first digit using regex pattern match = re.search(r'\d', str(num)) # 2: convert matched item to integer digit = int(match.group()) # 3: add sign back as needed return -digit if (num < 0) else digit print(first_digit(1234)) # 1 print(first_digit(-1234)) # -1 print(first_digit(0)) # 0 print(first_digit(-0)) # 0

## Using Floored Division

The following formula would give you the first digit of an integer:

quotient = floor(abs(integer) / integerLength - 1)

You can implement it in the following steps:

- Convert integer to absolute form;
- Get length of the integer to determine the divisor;
- Get the integer part from result of the division;
- Add back the minus sign if the number was originally negative.

def first_digit(num): # 1: convert to absolute form dividend = abs(num) # 2: get length of integer and determine divisor length = len(str(dividend)) divisor = 10 ** (length - 1) # 3: get integer part from result of division quotient = dividend // divisor # 4: add sign back as needed return -quotient if (num < 0) else quotient print(first_digit(1234)) # 1 print(first_digit(-1234)) # -1 print(first_digit(0)) # 0 print(first_digit(-0)) # 0

Since the dividend is in absolute form, floored division (`//`

) works for both, positive and negative, integers.

This works in the following way:

# num = -1234 # dividend = 1234 # length = 4 # divisor = 10 ^ (4 - 1) = 1000 # quotient = 1234 // 1000 # quotient = 1 # result = -1

## Reducing to Single Digit

You can loop over the number and reduce it in each iteration till *only* a single digit (i.e. digit less than `10`

) is left. This can be done in the following steps:

- Convert integer to absolute form;
- Reduce number to single digit:
- If integer is greater than
`10`

, then keep dividing the number by`10`

till a number less than`10`

is left, or; - If integer is less than
`10`

, then return it as is as it's*already*a single digit number.

- If integer is greater than
- Get the integer part of the resulting decimal number;
- Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

def first_digit(num): # 1: convert to absolute form abs_num = abs(num) # 2: reduce number to single digit while abs_num >= 10: abs_num /= 10 # 3: get integer part of decimal number # 4: add sign back as needed return int(-abs_num if (num < 0) else abs_num) print(first_digit(1234)) # 1 print(first_digit(-1234)) # -1 print(first_digit(0)) # 0 print(first_digit(-0)) # 0

## Converting to String and Retrieving First Character

You can convert the integer to string and get the first digit in the following steps:

- Convert integer in absolute form to string;
- Get the first character;
- Convert the numeric character back to integer;
- Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

def first_digit(num): # 1: convert absolute form to string num_str = str(abs(num)) # 2: get first character first_char = num_str[0] # 3: convert back to integer first_digit_unsigned = int(first_char) # 4: add sign back as needed return -first_digit_unsigned if (num < 0) else first_digit_unsigned print(first_digit(1234)) # 1 print(first_digit(-1234)) # -1 print(first_digit(0)) # 0 print(first_digit(-0)) # 0

Hope you found this post useful. It was published . Please show your love and support by sharing this post.