[Tutor] finding sum of digits in a string
Manprit Singh
manpritsinghece at gmail.com
Mon Mar 14 12:08:20 EDT 2022
Dear Sir,
Take an example of finding the sum of digits in a string :
st = "ab23YH6UJ7ik83LO"
To find the sum of digits in the string st, i can do it in below given
manners :
1) sum(int(ele) for ele in st if ele in "123456789")
2) sum(int(ele) for ele in st if ele.isdigit())
Although the 2nd one is more compact, using isdigit() method , what bothers
me
is - isdigit() method. It should be used to check a substring, not a single
character. Here I have to check all characters of the string one by one,
why not use in operator as done in the 1st way. Any guidelines regarding
this ?
Regards
Manprit Singh
More information about the Tutor
mailing list