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

When working with strings in Python, you may need to validate whether a string contains specific types of characters. This article demonstrates how to check if a string contains numbers, lowercase letters, and uppercase letters.

Problem Statement

Given a string, we need to check whether it contains all three of the following character types ?

  • Numbers (digits)

  • Lowercase letters

  • Uppercase letters

Note: The string may contain other symbols, but all three character types above must be present.

For example, if the input string is "p25KDs", the output should be True because it contains digits (2, 5), lowercase letters (p, s), and uppercase letters (K, D).

Algorithm

To solve this problem, we follow these steps ?

  • Create an array of size 3 initialized with False values
  • For each character in the string:
    • If the character is a digit, set arr[0] = True
    • If the character is lowercase, set arr[1] = True
    • If the character is uppercase, set arr[2] = True
  • Return True only if all elements in the array are True

Example Implementation

Here's the Python implementation of the validation function ?

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

# Test the function
s = "p25KDs"
print(solve(s))

The output of the above code is ?

True

How It Works

The function uses Python's built-in string methods:

  • isdigit() - returns True if the character is a digit
  • islower() - returns True if the character is a lowercase letter
  • isupper() - returns True if the character is an uppercase letter
  • all() - returns True if all elements in the list are True

Alternative Approach

You can also use the any() function with generator expressions for a more concise solution ?

def validate_string(s):
    has_digit = any(c.isdigit() for c in s)
    has_lower = any(c.islower() for c in s)
    has_upper = any(c.isupper() for c in s)
    
    return has_digit and has_lower and has_upper

# Test with different strings
test_strings = ["p25KDs", "hello", "HELLO123", "Hello123"]

for string in test_strings:
    result = validate_string(string)
    print(f"'{string}' contains all three types: {result}")

The output shows validation results for different test cases ?

'p25KDs' contains all three types: True
'hello' contains all three types: False
'HELLO123' contains all three types: False
'Hello123' contains all three types: True

Conclusion

This solution efficiently validates whether a string contains digits, lowercase letters, and uppercase letters using Python's built-in string methods. The algorithm runs in O(n) time complexity, making it suitable for strings of any length.

---
Updated on: 2026-03-26T15:29:49+05:30

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements