DEV Community

Cover image for isalpha() and isdigit()-Manually- NLP
datatoinfinity
datatoinfinity

Posted on

isalpha() and isdigit()-Manually- NLP

isalpha() is function built-in method which check whether all character in the string is alphabet.

 print("Heydude".isalpha()) print("H@y d1ude".isalpha()) 
 Output: True False 

Now we will do it manually, apply logic to make isalpha():

 def isalpha(text): counter=0 for i in text: if((ord(i)>=65 and ord(i)<=90) or (ord(i)>=97 and ord(i)<=122)): counter+=1 if (len(text)==counter): return True else: return False print(isalpha("Hey")) print(isalpha("123")) 
 True False 

Explanation:

  1. Take Counter to count character in string.
  2. Iterate through text.
  3. There conditions which check if alphabet lies in that ASCII range and it is for lower case and upper case.
  4. If there no alphabet then loop break and come out.
  5. Then check length of text and counter if it is same then return True or False

isdigit() is function built-in method which check whether all character in the string is digit.

 print("Heydude".isdigit()) print("234567".isdigit()) 
 Output: False True 

Now we will do it manually, apply logic to make isdigit():

 def isdigit(text): counter=0 for i in text: if((ord(i)>=48 and ord(i)<=57)): counter+=1 if (len(text)==counter): return True else: return False print(isdigit("Hey")) print(isdigit("123")) 
 False True 

Explanation:

  1. Take Counter to count character in string.
  2. Iterate through text.
  3. There conditions which check if digit lies in that ASCII range.
  4. If there no digit then loop break and come out.
  5. Then check length of text and counter if it is same then return True or False

Top comments (0)