n=input("Enter a Number :")
my_sum=0
for i in range(0,len(n)):
my_sum=my_sum+int(n[i])
print("Sum of digits in number : ", my_sum)
Output
Enter a Number :549
sum of digits in number : 18
In above code we used len() to get the number of elements ( or char here ) in the string object n.
n=int(input("Enter a Number :"))
my_sum=0
while(n>0):
i=n%10 # reminder value of division
my_sum=my_sum+i
n=n//10 # floor value of division
print("sum of digits in number=",my_sum)
Output
Enter a Number :1251
sum of digits in number=9
n=int(input("Enter a Number :"))
total=0
def my_sum(n):
global total
if n<=0:
return 0
else:
i=n%10 # reminder value of division
total=total+i
n=n//10 # floor value of division
my_sum(n) # Using recursive function
return total
print("sum of digits in number = ",my_sum(n))
We are using recursive function here. 
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.