计算字符串中所有数字的和 1.字符串中只有小写字母和数字 2.数字可能连续,也可能不连续 3.连续数字要当做一个数处理 如:'12abc34de5f' => 12 + 34 + 5 => 51 '''

s = '12abc34de5f'
def sum_of_num(s):
    num =0
    he = 0
    for i in s:
        if i.isdecimal():
            n=int(i)
            num = num*10 + n
        else:
            he = num + he
            num = num * 0
    if  s[-1].isdecimal():
        he = he + num
    return he
s = '12abc34de5fgh3'
print(sum_of_num(s))

猜你喜欢

转载自blog.csdn.net/LoveL_T/article/details/81281397
今日推荐