I am checking if each character within a string is alphanumeric and alphabetical. If all the characters within a string is alphanumeric/ alphabetical, I would print 'True', otherwise would print 'False'. My code is as below.
s = 'a2'
for i in s:
if i.isalnum():
print('True')
else:
print('False')
if i.isalpha():
print('True')
else:
print('False')
However, since I am using a For loop based on character within string, it would print out 4 results.
>>> Current output
True
True
True
False
But I want to print out result based on each string as a whole, so there should be only 2 results. Can anyone offer a help? Many thanks!!
>>>Expected output
True
False
CodePudding user response:
You can just use those functions on the entire string:
s = 'a2'
print(s.isalnum())
print(s.isalpha())
Output:
True
False
CodePudding user response:
You could do both functions isalnum() and isalpha() for the entire string
print(s.isalnum())
print(s.isaplha())