# Sum Of First And Last Digit Python

May 9, 2023 | Category : Other

Hey Artisan,Hello Developer,
In this quick example, let's see sum of first and last digit in python. I would like to share with you sum of first and last digit python. This article will give you a simple example of python sum of first and last digit of number. This article will give you a simple example of python program to find sum of first and last digit of a number. follow the below example for sum of first and last digit of a number in python.

There are several ways to sum first and last digit of number in python. Here's an example code in Python to find the sum of the first and last digits of a given integer:

Example 1:

main.py

`number1 = 43567    # Get First Digit from NumberlastDigit = str(number1)lastDigit = int(lastDigit)  # Get Last Digit from NumberfirstDigit = str(number1)[-1]firstDigit = int(firstDigit)  sum = firstDigit + lastDigit    print(sum)`

Output:

`11`

Example 2:

main.py

`num = int(input("Enter an integer: "))  # get input integer    # convert integer to string to access individual digitsnum_str = str(num)    # get the first and last digitsfirst_digit = int(num_str)last_digit = int(num_str[-1])    # calculate the sum of the first and last digitssum = first_digit + last_digit  print("The sum of the first and last digits of", num, "is", sum)`

In this code, we first prompt the user to input an integer. We then convert the integer to a string so that we can access the individual digits. We get the first digit using num_str and the last digit using num_str[-1]. Finally, we add the first and last digits and store the result in the variable sum, which we then print out.