﻿ Python: Sum of all numerical values embedded in a sentence - w3resource # Python: Sum of all numerical values embedded in a sentence

## Python Basic - 1: Exercise-56 with Solution

Write a Python program to sum of all numerical values (positive integers) embedded in a sentence.

Input:
Sentences with positive integers are given over multiple lines. Each line is a character string containing one-byte alphanumeric characters, symbols, spaces, or an empty line. However the input is 80 characters or less per line and the sum is 10,000 or less.

Sample Solution:

Python Code:

``````import sys,re
def test(stri):
print("Input some text and numeric values (<ctrl-d> to exit):")
print("Sum of the numeric values: ",sum([sum(map(int,re.findall(r"[0-9]{1,5}",stri)))]))

print(test("sd1fdsfs23 dssd56"))
print(test("15apple2banana"))
print(test("flowers5fruit5"))
``````

Sample Output:

```Input some text and numeric values ( to exit):
Sum of the numeric values:  80
None
Input some text and numeric values ( to exit):
Sum of the numeric values:  17
None
Input some text and numeric values ( to exit):
Sum of the numeric values:  10
None
```

Pictorial Presentation:  Flowchart: Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Python: The Zip() Function

```>>> students = ('John', 'Mary', 'Mike')
>>> ages = (15, 17, 16)
>>> scores = (90, 88, 82, 17, 14)
>>> for student, age, score in zip(students, ages, scores):
...     print(f'{student}, age: {age}, score: {score}')
...
John, age: 15, score: 90
Mary, age: 17, score: 88
Mike, age: 16, score: 82
>>> zipped = zip(students, ages, scores)
>>> a, b, c = zip(*zipped)
>>> print(b)
(15, 17, 16)
```