+5

How to add numbers in a string(such as:- abc123 which gives result as 6)?

11/14/2019 1:34:51 PM

Ayush Tripathi

4 Answers

New Answer

+8

a = 'a1b2c3d4' x = 0 for i in a: if i in '0123456789': x += int(i) print(x)

+6

# 2nd solution import re a = 'a1b2c3d4' print(sum([int(x) for x in re.findall('\d',a)]))

+4

A little late, but here is my try: a = 'a1b2c3d4' print(sum([int(i) for i in a if i.isdigit()])) # or in a for loop: x = 0 for i in a: if i.isdigit(): x += int(i) print(x)

+2