[Tutor] Python strings example
Manprit Singh
manpritsinghece at gmail.com
Mon Apr 26 11:48:24 EDT 2021
Dear sir,
consider a problem where I have to write a function that can return True if
the string passed to the function contains at least one digit character and
one alphabet character, else the function must return False. The way i am
solving it is given below :
def str_chk(word_tochk):
dig = False
alpha = False
for ele in word_tochk:
dig = dig or ele.isdigit()
alpha = alpha or ele.isalpha()
return dig and alpha
st = "Abhay2"
ans = str_chk(st)
print(ans) returns True, which is correct as the string contains an
alphabet as well as a digit character.
st = "Abhay"
ans = str_chk(st)
print(ans) returns False . which is also correct as there is no digit
character in the string now
Any suggestions, if the solution can be improved .
Regards
Manprit Singh
More information about the Tutor
mailing list