This forum is now read-only. Please use our new forums! Go to forums

# Different approach to digit summing

I am quite proud of my solution to digit_sum(x)

```
def digit_sum(x):
numString = str(x)
sum=0;
for digit in numString:
if digit.isdigit():
sum += int(digit)
return sum
```

This turns the number into a string, and then you can iterate through the string and convert each letter back to a number and sum. It also works if there is a minus sign or decimal because it uses pythonβs built-in function **.isdigit()** to check if the character is in fact a digit

## Answer 5097dfb45a49da0200000ad7

Have you seen this thread? There are some very neat solutions for this task. My favourite, because itβs so short and so βPythonyβ:

```
return sum( map( int, list( str(x) ) ) )
```

This one doesnβt do checking, of course. Your solution has the benefit of accepting both numbers and strings (and the strings can even contain non-digits). Nice!

## Answer 50a3ab5a76b0f32f25003fb0

I had the same approach, but didnβt think of using isdigit(), well done. I couldnβt think of an easy way to do it as an integer because without a len() how could the for loop be executed. Then I thought of using a while but it just seemed even more complex.

## Answer 51c55ee49c4e9d4105009c2e

## Answer 52d59375282ae3a99c000260

```
def digit_sum(n):
n = str(n)
total_count = 0
for i in range(len(n)):
total_count += int(n[i])
return total_count
```

## Answer 54f0552e86f552e543001775

I came up with:

```
def digit_sum(n):
total = 0
for i in str(n):
total += int(i)
return total
```

## Answer 509b5dc6da4f7d0200000513

I was gonna write about the same kind of solution, which also came natural to me.

## Popular free courses

- Free Course
### Learn SQL

In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,**4**LessonsLanguage Fluency - Free Course
### Learn JavaScript

Learn how to use JavaScript β a powerful and flexible programming language for adding website interactivity.Beginner friendly,**11**LessonsLanguage Fluency - Free Course
### Learn HTML

Start at the beginning by learning HTML basics β an important foundation for building and editing web pages.Beginner friendly,**6**LessonsLanguage Fluency

## 3 comments

Iβm intrigued. I am willing to submit to the reality that this solution will make sense to me as I keep studying, but, if you could point me in the right direction, Iβd appreciate it: what is this map()?

OMG! So smart! BTW, is your solution more efficient to run? I usually avoid this style if possible, because I bet I canβt read it in next morning (Iβm a noob!). But I would force myself to use this approach if it is more efficient :)

Very clever and a kind of coding style I really enjoy. However: from myβadmittedly limitedβunderstanding, this couldnβt be any less βPythonyβ. If you enter βimport thisβ into the interpreter, youβll see that this code violates several of the βZen of Pythonβ precepts, such as: β

Happy coding!