how to sum digits of a number in python

Do you want to know the details regarding “how to sum digits of a number in python”. If yes, you’re in the correct article.

how to sum digits of a number in python

number = 123 # the number you want summed up
sum_of_digits = 0 
for digit in str(number):
  sum_of_digits += int(digit)
  
print(sum_of_digits) # printing the final sum of number
number = 123 # the number you want to sum the digits of

sum_of_digits = sum(int(digit) for digit in str(number))

print(sum_of_digits)
def sum_digits(num):
	return sum(map(int, str(num)))

Conclusion

I hope this article helps you to know about “how to sum digits of a number in python”. If you have any doubts regarding this tutorial please let us know via the comment section. Share this tutorial with your friends & family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment