Python: Check whether a given string has a capital letter, a lower case letter, a number and specified length
Check string for capital, lower, number, length.
Write a Python program to check whether a given string contains a capital letter, a lower case letter, a number and a minimum length.
Sample Solution:
Python Code:
# Define a function to check the validity of a string based on certain criteria
def check_string(s):
# Initialize an empty list to store error messages
messg = []
# Check if the string contains at least one uppercase character
if not any(x.isupper() for x in s):
messg.append('String must have 1 upper case character.')
# Check if the string contains at least one lowercase character
if not any(x.islower() for x in s):
messg.append('String must have 1 lower case character.')
# Check if the string contains at least one digit
if not any(x.isdigit() for x in s):
messg.append('String must have 1 number.')
# Check if the string length is at least 8 characters
if len(s) < 8:
messg.append('String length should be at least 8.')
# If there are no error messages, add a message indicating the string is valid
if not messg:
messg.append('Valid string.')
# Return the list of error messages or the validation message
return messg
# Get user input for a string
s = input("Input the string: ")
# Call the function to check the string and print the result
print(check_string(s))
Sample Output:
Input the string: W3resource ['Valid string.']
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to validate that an input string contains at least one uppercase letter, one lowercase letter, one digit, and meets a minimum length requirement.
- Write a Python program to use regular expressions to verify that a string satisfies conditions for capital letters, lowercase letters, numbers, and length.
- Write a Python program to implement a function that returns a list of missing criteria if an input string fails to meet specified requirements.
- Write a Python program to prompt the user for a string and print "Valid string." only if all character type conditions are met.
Python Code Editor:
Previous: Write a Python program find the common values that appear in two given strings.
Next: Write a Python program to remove unwanted characters from a given string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics