w3resource

Python: Check whether a given string has a capital letter, a lower case letter, a number and specified length using lambda


33. Password Checker Lambda

Write a Python program to check whether a given string contains a capital letter, a lower case letter, a number and a minimum length using lambda.

Sample Solution:

Python Code :

# Define a function 'check_string' that takes a string 'str1' as input
def check_string(str1):
    # Define a list 'messg' containing lambda functions with string validation conditions
    messg = [
        lambda str1: any(x.isupper() for x in str1) or 'String must have 1 upper case character.',
        lambda str1: any(x.islower() for x in str1) or 'String must have 1 lower case character.',
        lambda str1: any(x.isdigit() for x in str1) or 'String must have 1 number.',
        lambda str1: len(str1) >= 7 or 'String length should be at least 8.',
    ]
    
    # Generate a list 'result' by evaluating each lambda function in 'messg' for the input string 'str1'
    result = [x for x in [i(str1) for i in messg] if x != True]
    
    # Check if 'result' list is empty (all validation conditions are met)
    if not result:
        # If 'result' is empty, append 'Valid string.' indicating the string passes all validations
        result.append('Valid string.')
    
    # Return the 'result' list containing validation messages or 'Valid string.' if the string is valid
    return result    

# Get user input for a string 's'
s = input("Input the string: ")

# Print the result of string validation by calling the 'check_string' function with input 's'
print(check_string(s)) 

Sample Output:

Input the string:  W3resource
['Valid string.']

For more Practice: Solve these Related Problems:

  • Write a Python program to validate if a string contains at least one special character, one number, and both uppercase and lowercase letters using lambda.
  • Write a Python program to check if a string contains no whitespace, includes at least one digit, and at least one uppercase letter using lambda.
  • Write a Python program to verify if a string qualifies as a strong password (uppercase, lowercase, digit, special character, minimum length 8) using lambda.
  • Write a Python program to check if a string contains exactly two uppercase letters, three lowercase letters, and one digit using lambda.

Go to:


Previous: Write a Python program to count float number in a given mixed list using lambda.
Next: Write a Python program to filter the height and width of students, which are stored in a dictionary using lambda.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.