Skip to content

Instantly share code, notes, and snippets.

@hoanbka
Created October 24, 2017 19:42
Show Gist options
  • Save hoanbka/9ffa7cbc204fdac29f13fedecf1a102c to your computer and use it in GitHub Desktop.
Save hoanbka/9ffa7cbc204fdac29f13fedecf1a102c to your computer and use it in GitHub Desktop.
sum of digits in a string
def sumOfDigits(str):
temp = ''
sum = 0
for i in range(len(str)):
if str[i].isdigit():
temp += str[i]
else:
if temp.isdigit():
sum += int(temp)
temp = ''
if temp.isdigit():
sum += int(temp)
return sum
#TEST
str = '123adgg2sds3'
print(sumOfDigits(str))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment