Python: Check whether a given string has a capital letter, a lower case letter, a number and specified length using lambda
Python Lambda: Exercise-33 with Solution
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.']
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/lambda/python-lambda-exercise-33.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics