Python: Check whether a given string has a capital letter, a lower case letter, a number and specified length
Python String: Exercise-88 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.
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:
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.
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://198.211.115.131/python-exercises/string/python-data-type-string-exercise-88.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics