Learn about Get First Digit of Number in Python using Slicing in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Get First Digit of Number in Python using Slicing
# Program to get the first digit of number
# input
num = int(input('Enter any Number: '))
# convert int to string
num_str = str(num)
# access first digit
first_digit = num_str[:1]
# printing first digit
print('The first digit of number:', first_digit)
Output:
Enter any Number: 234
The first digit of number: 2
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : First Digit of Number using Built-in Function In Python