• 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
  • Ruby
  • 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.  Change Python Turtle Shape Fill Color with fillcolor() Function
  • 2.  Check if Character is Letter in Python
  • 3.  Remove Leading Zeros from String with lstrip() in Python
  • 4.  Using Python to Check If a Number is a Perfect Square
  • 5.  Python Print List – Printing Elements of List to the Console
  • 6.  Convert True to 1 in Python
  • 7.  Check if Number is Between Two Numbers Using Python
  • 8.  How to Remove All Occurrences of a Character in a List Using Python
  • 9.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 10.  String Contains Case Insensitive in Python

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy