Hanwool Codes RSS Tag Admin Write Guestbook
2023-02-16 05:42:04

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")
Hanwool Codes. Designed by 코딩재개발.