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

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

Example

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"
print(solve(s))

Input

"p25KDs"

Output

True
Updated on: 2021-10-11T11:01:37+05:30

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements