How to extract and sum all numbers from a string in Python

1 Answer

0 votes
import re

s = "abc 4 xyz 8 poq 9 www=23"

numbers = [int(num) for num in re.findall(r'\b\d+\b', s)]

print(numbers)

sum = 0
for n in numbers:
  sum += n

print(sum)


'''
run:

[4, 8, 9, 23]
44

'''

 



answered Apr 10, 2019 by avibootz

Related questions

...