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


The program takes a string as input, and then checks whether it meets certain criteria for being a valid string. The program then prints the validation results to the console.

  • The program first prompts the user to enter a string using the input() function, and stores the input in a variable s.
  • The program then initializes an empty list res that will be used to store the validation results.
  • Next, the program checks if the input string contains at least one uppercase character using the any() function with a generator expression. If there is no uppercase character in the string, the program appends a validation message to the res list indicating that the string must have at least one uppercase character.
  • The program then checks if the input string contains at least one lowercase character using a similar any() function call. If there is no lowercase character in the string, the program appends a validation message to the res list indicating that the string must have at least one lowercase character.
  • The program then checks if the input string contains at least one digit using a similar any() function call. If there is no digit in the string, the program appends a validation message to the res list indicating that the string must have at least one digit.
  • The program then checks if the length of the input string is less than 8 characters, and if it is, appends a validation message to the res list indicating that the string must be at least 8 characters long.
  • If none of the validation criteria fail, the program appends a validation message to the res list indicating that the string is valid.
  • Finally, the program prints the validation results to the console using a loop that iterates over the res list and prints each validation message.

Source Code

s = input("Enter the string: ")
res = []
if not any(x.isupper() for x in s):
	res.append("String must have 1 Uppercase Character.")
if not any(x.islower() for x in s):
	res.append("String must have 1 Lowercase Character.")
if not any(x.isdigit() for x in s):
	res.append("String must have 1 Number.")
if len(s) < 8:
	res.append("String length should be atleast 8.")    
if not res:
	res.append("Valid string.")
 
print("")
for i in res:
	print(i)

Output

Enter the string: Python

String must have 1 Number.
String length should be atleast 8.


Example Programs