Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc.
str.isalnum()
This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).
This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).
>>> print 'ab123'.isalnum()
True
>>> print 'ab123#'.isalnum()
False
>>> print 'abcD'.isalpha()
True
>>> print 'abcd1'.isalpha()
False
>>> print '1234'.isdigit()
True
>>> print '123edsd'.isdigit()
False
>>> print 'abcd123#'.islower()
True
>>> print 'Abcd123#'.islower()
False
>>> print 'ABCD123#'.isupper()
True
>>> print 'Abcd123#'.isupper()
False
Task
You are given a string .
Your task is to find out if the string contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.
Your task is to find out if the string contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.
Input Format
A single line containing a string .
Constraints
Output Format
In the first line, print
In the second line, print
In the third line, print
In the fourth line, print
In the fifth line, print
True
if has any alphanumeric characters. Otherwise, print False
.In the second line, print
True
if has any alphabetical characters. Otherwise, print False
.In the third line, print
True
if has any digits. Otherwise, print False
.In the fourth line, print
True
if has any lowercase characters. Otherwise, print False
.In the fifth line, print
True
if has any uppercase characters. Otherwise, print False
.
Sample Input
qA2
Sample Output
True
True
True
True
True
String Validators - Hacker Rank Solution
Check the string by using the methods: .isalpha(), .isalnum(), .isdigit(), .islower() and .isupper().
Problem Setter's code:
S = raw_input()
print any([char.isalnum() for char in S])
print any([char.isalpha() for char in S])
print any([char.isdigit() for char in S])
print any([char.islower() for char in S])
print any([char.isupper() for char in S])
correct code for given problem
S = input()
print(any(char.isalnum() for char in S))
print(any(char.isalpha() for char in S))
print(any(char.isdigit() for char in S))
print(any(char.islower() for char in S))
print(any(char.isupper() for char in S))
yes for python3 >
Deleteraw_input is python2 you should know this lol
Shut up
Deleteyaa
ReplyDeletenice
ReplyDeletes = input()
ReplyDeleteprint(any(map(str.isalnum,s)))
print(any(map(str.isalpha,s)))
print(any(map(str.isdigit,s)))
print(any(map(str.islower,s)))
print(any(map(str.isupper,s)))
if __name__ == '__main__':
ReplyDeletes = input()
print(any(a.isalnum() for a in s))
print(any(a.isalpha() for a in s))
print(any(a.isdigit() for a in s))
print(any(a.islower() for a in s))
print(any(a.isupper() for a in s))
s = input()
ReplyDeletealnm = 'False'
alp = 'False'
dig = 'False'
low = 'False'
upp = 'False'
for i in s:
if i.isalnum():
alnm = 'True'
if i.isalpha():
alp = 'True'
if i.isdigit():
dig = 'True'
if i.islower():
low = 'True'
if i.isupper():
upp = 'True'
print(alnm + '\n'+ alp + '\n'+dig + '\n'+ low+ '\n'+upp)
if __name__ == '__main__':
ReplyDeletes = input()
alphanum=False
alpha=False
digit=False
lrcase=False
upcase=False
for each in s:
if each.isalnum():
alphanum=True
if each.isalpha():
alpha=True
if each.isdigit():
digit=True
if each.islower():
lrcase=True
if each.isupper():
upcase=True
print(alphanum,alpha,digit,lrcase,upcase, sep='\n' )
Pichilepisnav bro thop.
Delete
ReplyDeleteif __name__ == '__main__':
s = input()
print(any(char.isalnum() for char in s))
print(any(char.isalpha() for char in s))
print(any(char.isdigit() for char in s))
print(any(char.islower() for char in s))
print(any(char.isupper() for char in s))
Most Complicated solution
ReplyDeletes = input()
isn = []
isa = []
isd = []
isl = []
isu = []
for i in s:
isn.append(i.isalnum())
isa.append(i.isalpha())
isd.append(i.isdigit())
isl.append(i.islower())
isu.append(i.isupper())
print('True'if(isn.count(isn[0]) != len(isn))else isn[0])
print('True'if(isa.count(isa[0]) != len(isa))else isa[0])
print('True'if(isd.count(isd[0]) != len(isd)) else isd[0])
print('True'if(isl.count(isl[0]) != len(isl))else isl[0])
print('True'if(isu.count(isu[0]) != len(isu))else isu[0])