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:
- Take Counter to count character in string.
- Iterate through text.
- There conditions which check if alphabet lies in that ASCII range and it is for lower case and upper case.
- If there no alphabet then loop break and come out.
- 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:
- Take Counter to count character in string.
- Iterate through text.
- There conditions which check if digit lies in that ASCII range.
- If there no digit then loop break and come out.
- Then check length of text and counter if it is same then return True or False
Top comments (0)