How to extract digits from a Python string and add them together?

Asked

Viewed 15,696 times

11

I need to decompose one string in Python, separating letter and numbers, and summing these numbers. For example:

string = "96h11k"

Of that string need to extract the numbers 9, 6, 1, 1 and add them: 9 + 6 + 1 + 1 = 17. In fact, the sum happens between the digits.

  • 1

    It helps you? str.isdigit() Or do you want a more complete example? Do you already have some code started that is causing you difficulties? (in other words, tell us what you already know and what you still need to know. By the way, is this some exercise, or is it to be used in practice? I can give you one one-Liner that does this, or I can explain in more detail, with loops, etc.)

  • 1

    It’s just an exercise. I need to improve string manipulation. I can manipulate numbers, but I can’t really manipulate strings, so I saw this problem and I’m trying to solve it...

  • For example... the function str.isdigit() returns True or False... in case I would have to split the string and check each digit?

  • That’s right. You can do this with loops or - if you already know the concept - list understandings. I’m writing a response.

4 answers

13

According to that response in the OS could use functions ready for this:

def sum_digits(digit):
    return sum(int(x) for x in digit if x.isdigit())

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

This way you are using the function sum() whose data are generated through a list comprehension where each element of the list (a string is a list of characters) is broken down individually.

In this case you are taking the parameter digit and analyzing each of its elements, that is, each of the characters. If the taken element is considered a numerical digit (function isdigit()) it will be delivered to the function algorithm sum() who knows how to add them.

You can also do it manually as shown by response from mgibsonbr.

12


In Python, you can iterate on a string as if it were a list:

>>> for ch in string:
...   print(ch)
...
9
6
h
1
1
k

In the loop above, with each iteration ch will be a string 1-character. To find out if this character is numeric or not, you can use ch.isdigit(). To convert it to an integer, you can use int(ch). So just add it all up:

soma = 0
for ch in string:
    if ch.isdigit():
        soma += int(ch)
print(soma)

A more concise way to do this is by using list comprehensions (or generating expressions), and the function sum() (sum). For an example, see the response of Maniero.

  • thank you so much! It worked out! I still can’t vote on your answer, I don’t have 15 reputation points... :-/

3

my_string = "96h11k"
soma = sum([int(i) for i in my_string if i.isdigit()])

--------EDIT---------

Now that I see that my answer is identical to Maniero...

For the answer not to be completely useless, there goes a reading on Generator Expressions.

  • 1

    Thank you for your cooperation!

1

fim=int(input("Digite o valor de n: "))
string = str(fim)
soma = 0
for ch in string:
    if ch.isdigit():
        soma += int(ch)
print(soma)    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.