String Validators | HackerRank
String Validators | HackerRank
Identify the presence of alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters in a string.
www.hackerrank.com
Goal
Find out if the give string S contains : alphanumeric characters, alphabetical characters, digits, lowercase and uppercaser characters.
Solution
You can use built-in string validation methods to solve this solution.
The script initializes a boolean variable is_pass to False, which will be used to determine whether or not a given test has passed.
The first loop in the script iterates over each character in the input string s.
For each character, it checks whether or not it is alphanumeric using the isalnum() method.
If it is, the script prints "True", sets is_pass to True, and breaks out of the loop.
If the loop completes without finding an alphanumeric character, the script prints "False".
The next four loops work similarly, but check for alphabetical characters(isalpha()), digits(isdigit()), lowercase characters(islower()), and uppercase characters(isuppper()), respectively.
After each loop, the script checks whether or not is_pass is still False.
If it is, this means that the test has not been passed, so the script prints "False".
# String Validators
if __name__ == '__main__':
s = input()
is_pass = False
for c in s:
if c.isalnum():
print("True")
is_pass = True
break
if(is_pass == False):
print("False")
is_pass = False
for c in s:
if c.isalpha():
print("True")
is_pass = True
break
if(is_pass == False):
print("False")
is_pass = False
for c in s:
if c.isdigit():
print("True")
is_pass = True
break
if(is_pass == False):
print("False")
is_pass = False
for c in s:
if c.islower():
print("True")
is_pass = True
break
if(is_pass == False):
print("False")
is_pass = False
for c in s:
if c.isupper():
print("True")
is_pass = True
break
if(is_pass == False):
print("False")
'software Engineering > (Python)HackerRank' 카테고리의 다른 글
[Python] HackerRank - String Formatting (0) | 2023.02.18 |
---|---|
[Python] HackerRank - Text Wrap (0) | 2023.02.17 |
[Python] HackerRank - Find a string (0) | 2023.02.15 |
[Python] HackerRank - Mutations (0) | 2023.02.14 |
[Python] HackerRank - What's Your Name? (0) | 2023.02.11 |