Skip to content
Home » Sum of Digits of a Number using List in Python

Sum of Digits of a Number using List in Python

Learn about Sum of Digits of a Number using List in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

Sum of Digits of a Number using List in Python

# Python program to compute sum of digits in number

def ComputeSum(num):
    k = str(num) 
    list_num = list(map(int, k.strip())) 
    return sum(list_num)
    
# input from user
num = int(input('Enter a number: '))

# function call
print('Sum of digits in number =', ComputeSum(num))

Output:

Enter a number: 234
Sum of digits in number = 9

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Python Program to Find Sum of Digits using Recursion