• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Sum the Digits of a Number

Using Python to Sum the Digits of a Number

May 5, 2022 Leave a Comment

To sum the digits of a number in Python, you can use a loop which will get each digit and add them together.

def sumDigits(num):
    sum = 0
    for x in str(num):
        sum = sum + int(x)
    return sum

print(sumDigits(100))
print(sumDigits(213))

#Output:
1
6

When working with numbers in Python, the ability to easily get information and statistics from them is valuable.

For example, one interesting case is if you want to get the sum of the digits of a number.

We can easily get the sum of the digits of a number in Python by first splitting a number into its digits and then summing up the digits using a loop.

To get the digits of a number, we first convert the number to a string and loop over that string. Then for each digit, we add it to a running total.

Below is a simple example in Python of how you can add up all of the digits of a number using a loop.

def sumDigits(num):
    sum = 0
    for x in str(num):
        sum = sum + int(x)
    return sum

print(sumDigits(100))
print(sumDigits(213))

#Output:
1
6

Hopefully this article has been useful for you to learn how to sum the digits of a number in Python.

Other Articles You'll Also Like:

  • 1.  Python Round to Nearest 10 With round() Function
  • 2.  Python Get Operating System Information with os and platform Modules
  • 3.  Python Delete Variable – How to Delete Variables with del Keyword
  • 4.  Python Factorial Recursion – Using Recursive Function to Find Factorials
  • 5.  Add Minutes to Datetime Variable Using Python timedelta() Function
  • 6.  Zip Two Lists in Python
  • 7.  Creating a Random Color Turtle in Python
  • 8.  Python Add Days to Date Using datetime timedelta() Function
  • 9.  PROC MIXED Equivalent in Python for Least Squared Means ANOVA
  • 10.  Truncate String in Python with Slicing

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy