Sum the digits of a number in Python

avatar

Borislav Hadzhiev

Last updated: Jul 3, 2022

banner

Photo from Unsplash

Sum the digits of a number in Python #

To sum the digits of a number:

  1. Use the str() class to convert the number to a string.
  2. Use a generator expression to iterate over the string.
  3. On each iteration, convert the digit to a number with the int() class.
  4. Pass the generator object to the sum() function.
main.py
num = 135 result_1 = sum(int(digit) for digit in str(num)) print(result_1) # 👉️ 9 # -------------------------------------------------- result_2 = sum(map(int, str(num))) print(result_2) # 👉️ 9 # --------------------------------------------------- # ✅ sum the digits of a number without converting to string result_3 = 0 while num: result_3 += num % 10 print(result_3) num = num // 10 print(result_3) # 👉️ 9

The first example uses the str() class and a generator expression to sum the digits in a number.

main.py
num = 135 result_1 = sum(int(digit) for digit in str(num)) print(result_1) # 👉️ 9

Strings are iterable and integers are not, so we have to convert the integer to a string.

Generator expressions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we convert the digit that is wrapped in a string to an integer and return the result.

The last step is to pass the generator object to the sum() function.

The sum function takes an iterable, sums its items from left to right and returns the total.

The second example uses the map() function instead of a generator expression for the conversion.

main.py
num = 135 result_2 = sum(map(int, str(num))) print(result_2) # 👉️ 9

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The map function calls the int() class with each digit in the string and returns a map object that we can directly pass to the sum() function.

Alternatively, you can use a while loop.

To sum the digits of a number:

  1. Initialize a variable for the sum and set it to 0.
  2. Use a while loop to iterate while the number variable is truthy.
  3. Get the rightmost digit of the number with num % 10 and add it to the sum variable.
  4. Remove the rightmost digit by floor-dividing by 10.
main.py
num = 135 result = 0 while num: result += num % 10 num = num // 10 print(result) # 👉️ 9

We used a while loop to iterate while the num variable stores a truthy value.

0 is a falsy value, so as soon as the num variable gets set to 0, the condition is no longer met.

We used the modulo % operator to get the rightmost digit of the number.

The modulo (%) operator returns the remainder from the division of the first value by the second.

main.py
print(135 % 10) # 👉️ 5 print(13 % 10) # 👉️ 3

Once we reassign the result variable to its current value plus the rightmost digit of the number, we have to remove the rightmost digit.

We can remove the rightmost digit of a number by using floor division // with 10.

main.py
print(135 // 10) # 👉️ 13 print(13 // 10) # 👉️ 1

Division / of integers yields a float, while floor division // of integers result in an integer.

The result of using the floor division operator is that of a mathematical division with the floor() function applied to the result.

Eventually 1 gets floor-divided by 10, which returns 0, and the while loop stops iterating.

main.py
num = 135 result = 0 while num: result += num % 10 num = num // 10 print(result) # 👉️ 9 print(num) # 👉️ 0
The code snippet above reassigns the value of the num variable on each iteration and eventually sets it to 0.

If you need to keep the variable around, make sure to make a copy.

main.py
num = 135 # 👇️ make copy of num num_copy = num result = 0 while num_copy: result += num_copy % 10 num_copy = num_copy // 10 print(result) # 👉️ 9 print(num_copy) # 👉️ 0 print(num) # 👉️ 135
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.