+ 5

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

14th Nov 2019, 1:34 PM
Ayush Tripathi
Ayush Tripathi - avatar
2 Answers
+ 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)
14th Nov 2019, 3:27 PM
Lothar
Lothar - avatar
+ 2
Thanks
14th Nov 2019, 1:54 PM
Ayush Tripathi
Ayush Tripathi - avatar