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 //.
- Turning a number into its digits
- sum() with a generator
- The arithmetic version with % 10 and // 10
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.
str(abs(number))🪧 Write the number on a sign as textfor digit in ...👆 Point at each character in turnint(digit)🔢 Turn the character back into a numbersum(...)➕ Add them all: 15The digit sum of a number is the total of its individual digits, such as 1 + 2 + 3 + 4 + 5 = 15 for 12345.
Key points
abs(number)removes a minus sign, which is not a digit.str(abs(number))turns the number into text, so each character is one digit.int(digit)converts each character back into a number.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 % 10andn // 10avoid creating strings. - Numbers with decimal points: the dot is not a digit, so
int()would fail on it.
Example code
number = 12345
# Convert each digit character back to an integer.
total = sum(int(digit) for digit in str(abs(number)))
print(f"sum={total}")
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.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Adding the characters instead of the numbers
Characters joined with + give text, not a sum.
total = ""
for digit in str(number):
total += digit # "12345"
total = sum(int(digit) for digit in str(abs(number)))
Forgetting the minus sign
int("-") raises ValueError for negative numbers.
sum(int(digit) for digit in str(number))
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.
- Sum of Digits Add up the decimal digits of a positive integer. Easy
- Digital Root Sum the digits repeatedly until one digit remains. Medium