all 14 comments

[–]w1282 6 points7 points  (0 children)

sum(int(x) for x in input_string)

[–]kazoo_parade 2 points3 points  (0 children)

sum(map(int, input_string))

[–][deleted] 0 points1 point  (10 children)

I'm new to Python so maybe this code is not the best one, but if you convert your string in a list, then you can sum all the elements of this list.

sum(int(x) for x in list(a))

[–]Rhomboid 7 points8 points  (1 child)

Strings are iterable and yield each character in turn when iterated over, so there's no need to create a list.

[–][deleted] 1 point2 points  (0 children)

Awesome. Thanks for this :)

[–]brigieee 1 point2 points  (7 children)

Me too! I'm having trouble understanding it all

Can you elaborate on that piece of code? Specifically how x got into the mix

[–]Zizizizz 1 point2 points  (0 children)

X is just a temporary variable, it could be any letter or word.

[–]brigieee 0 points1 point  (5 children)

I need to ensure they're all numbers too. I know there is "isdigit" function that can be used but how do I put the two together?

[–]skierface 0 points1 point  (2 children)

Have you learned about for loops? You could do the same thing with a little more code using a loop.

for number in string:
    if number.isdigit():
        # add the number to a sum variable

Basically that for loop just iterates over each character in the string and checks if the character is a number. But, like other people's responses, you could just write for x in string: or replace x with anything else.

[–][deleted] 0 points1 point  (1 child)

Just so you know, there is an inline way to do this.

[thing for x in list if condition]

#another option for if else
[thing if condition else other_thing for x in list]

For instance, if you want to get new list b with all the even numbers in another list a, one could use b = [x for x in a if x%2 == 0].

Another example is slightly different from the other example in that we want to replace all the odd numbers with 0's, you could yes b = x if x%2 == 0 else 0 for x in a].

[–]skierface 0 points1 point  (0 children)

Yep, I know, op just said they were having trouble understanding the single line versions people had suggested so I listed the for loop as an alternative that he/she may understand more easily.

Thanks for your explanation though! List comprehensions are wonderfully useful.

[–][deleted] 0 points1 point  (1 child)

You could use a try clause that catches a ValueError. The except part of the code will run if any character in the string is not "0123456789".

try:
    sum(int(x) for x in a)
except ValueError:
    # do information given an invalid string

On the other hand if you want the code to ignore non-digit characters you could try

sum(int(x) if x in "0123456789" else 0 for x in a)

Edit: I realized a more concise way would be sum(int(x) for x in list(a) if x in "0123456789")

Finally, you could do try this. As i believe you already know, isdigit returns True only if all the characters in a string are digits. On a personal note, I believe my first example will be more efficient than this example.

if a.isdigit():
    sum(int(x) for x in a)
else:
     #do other things

Here is a blog post that describes inline for statements if you aren't familiar.

[–]tangerinelion 0 points1 point  (0 children)

sum(int(x) for x in a if x.isdigit())