Python program to validate string has few selected type of characters or not

Suppose we have a string s. We have to check whether the string contains the following or not.

  • Numbers

  • Lowercase letters

  • Uppercase letters

Note − There may be some other symbols, but these three must be there

So, if the input is like s = "p25KDs", then the output will be True

To solve this, we will follow these steps −

  • arr := an array of size 3 and fill with False
  • for each character c in s, do
    • if c is alphanumeric, then
      • arr[0] := True
    • if c is in lowercase, then
      • arr[1] := True
    • if c is in uppercase, then
      • arr[2] := True
  • return true when all items of arr are true


Let us see the following implementation to get better understanding

def solve(s):
   arr = [False]*3
   for c in s:
      if c.isalnum():
         arr[0] = True
      if c.islower():
         arr[1] = True
      if c.isupper():
          arr[2] = True

   return all(arr)

s = "p25KDs"