Beginner Python Exercises

Sum the Digits of a Number in Python

12345 → 1 + 2 + 3 + 4 + 5 = 15. Do it by turning the number into text, or with % and //.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Turning a number into its digits
  • sum() with a generator
  • The arithmetic version with % 10 and // 10
12345
11
23
36
410
515
The small numbers show the running total: sum = 15.

Visit every digit

The problem

Check digits on credit cards and ISBNs, digital roots and many puzzles need the digits of a number added together. A number is not a list, so you first need a way to look at its digits one by one.

Think of it like this

Summing digits by converting to text is like reading a house number off a sign. As text, 12345 is just five characters you can point at one by one. You read each character, turn it back into a number and add it to the total.

From a house number sign to code
str(abs(number))🪧 Write the number on a sign as text
for digit in ...👆 Point at each character in turn
int(digit)🔢 Turn the character back into a number
sum(...)➕ Add them all: 15
As text, a number becomes a row of digits you can visit one by one.
In one sentence

The digit sum of a number is the total of its individual digits, such as 1 + 2 + 3 + 4 + 5 = 15 for 12345.

Key points

  1. abs(number) removes a minus sign, which is not a digit.
  2. str(abs(number)) turns the number into text, so each character is one digit.
  3. int(digit) converts each character back into a number.
  4. sum(...) adds the converted digits and gives 15.

When should you use it?

  • Check-digit rules such as the Luhn algorithm for card numbers.
  • Digit puzzles: digital roots, Armstrong numbers, divisibility by 3 or 9.
  • Practicing conversion between numbers and text.

When not to use it

  • Performance-critical code with many numbers: n % 10 and n // 10 avoid creating strings.
  • Numbers with decimal points: the dot is not a digit, so int() would fail on it.

Example code

Run code →
main.py
number = 12345
# Convert each digit character back to an integer.
total = sum(int(digit) for digit in str(abs(number)))
print(f"sum={total}")
Get a Run button for this example

Result of running the code

sum=15

Code explanation

abs(number) removes the sign so that negative numbers work, and str() turns the result into text such as "12345". The generator expression goes through that text one character at a time and int(digit) turns each character back into a number. sum() adds those numbers, which gives 15 here.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Adding the characters instead of the numbers

Characters joined with + give text, not a sum.

Problem
total = ""
for digit in str(number):
    total += digit  # "12345"
Fix
total = sum(int(digit) for digit in str(abs(number)))

Forgetting the minus sign

int("-") raises ValueError for negative numbers.

Problem
sum(int(digit) for digit in str(number))
Fix
sum(int(digit) for digit in str(abs(number)))

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges