String Validators in Python - HackerRank Solution
Problem :
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.
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
This method checks if all the characters of a string are lowercase characters (a-z).
>>> print 'abcd123#'.islower() True >>> print 'Abcd123#'.islower() False
>>> print 'ABCD123#'.isupper() True >>> print 'Abcd123#'.isupper() False
Task :
You are given a string S.
Your task is to find out if the string S contains: alphanumeric characters,
alphabetical characters, digits, lowercase and uppercase characters.
Input Format :
A single line containing a string S.
Constraints :
- 0 <= len(s) <= 1000
Output Format :
In the first line, print True if S has any alphanumeric characters. Otherwise,
print False.
In the second line, print True if S has any alphabetical characters. Otherwise, print False.
In the third line, print True if S has any digits. Otherwise, print False.
In the fourth line, print True if S has any lowercase characters. Otherwise, print False.
In the second line, print True if S has any alphabetical characters. Otherwise, print False.
In the third line, print True if S has any digits. Otherwise, print False.
In the fourth line, print True if S has any lowercase characters. Otherwise, print False.
In the fifth line, print True if S has any uppercase characters. Otherwise,
print False.
Sample Input :
qA2
Sample Output :
True True True True True
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | # String Validators in Python - HackerRank Solution # String Validators in Python - HackerRank Solution START def fun1(s): for i in range(len(s)): if(s[i].isalnum()): return True; break; return False; def fun2(s): for i in range(len(s)): if(s[i].isalpha()): return True; break; return False; def fun3(s): for i in range(len(s)): if(s[i].isdigit()): return True; break; return False; def fun4(s): for i in range(len(s)): if(s[i].islower()): return True; break; return False; def fun5(s): for i in range(len(s)): if(s[i].isupper()): return True; break; return False; # String Validators in Python - HackerRank Solution END if __name__ == '__main__': s = raw_input() # String Validators in Python - HackerRank Solution START flagalphanum = fun1(s) alphabetical = fun2(s) digits = fun3(s) lowercase = fun4(s) uppercase = fun5(s) print(flagalphanum) print(alphabetical) print(digits) print(lowercase) print(uppercase) # String Validators in Python - HackerRank Solution END |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
break is not required if using return statement
print(True in list(map(lambda x:x.isalnum(),s)))
print(True in list(map(lambda x:x.isalpha(),s)))
print(True in list(map(lambda x:x.isdigit(),s)))
print(True in list(map(lambda x:x.islower(),s)))
print(True in list(map(lambda x:x.isupper(),s)))
def test1(s):
for i in range(len(s)):
if s[i].isalnum():
return True
return False
def test2(s):
for i in range(len(s)):
if s[i].isalpha():
return True
return False
def test3(s):
for i in range(len(s)):
if s[i].isdigit():
return True
return False
def test4(s):
for i in range(len(s)):
if s[i].islower():
return True
return False
def test5(s):
for i in range(len(s)):
if s[i].isupper():
return True
return False
if __name__ == '__main__':
s = input()
result1 = test1(s)
result2 = test2(s)
result3 = test3(s)
result4 = test4(s)
result5 = test5(s)
print(result1)
print(result2)
print(result3)
print(result4)
print(result5)
s = input()
list_method=['isalnum()','isalpha()','isdigit()','islower()','isupper()']
for i in list_method:
for j in s:
if eval(f"'{j}'.{i}"):
print(True)
break
so nice i tried using eval but not worked
thank you
This comment has been removed by the author.
i don't know why you put semicolon ; on python