How to check if a string only contains certain characters in Python?

To check if a Python string contains only certain characters, you can use several approaches: set comparison, regular expressions, and character validation with loops. These methods help you verify whether every character in the string belongs to a defined set of allowed characters.

Using Set Comparison

You can create a set of allowed characters and check if all characters in the string belong to that set using issubset(). If the string characters form a subset of the allowed set, it means the string contains only valid characters ?

def check_acceptable_chars(text, allowed_chars):
    validation = set(text)
    print("Checking if it contains only", allowed_chars)
    return validation.issubset(allowed_chars)

acceptable_chars = set('0123456789')

str1 = "1234654185"
print("The given string is:", str1)
print(check_acceptable_chars(str1, acceptable_chars))

str2 = "12346@"
print("The given string is:", str2)
print(check_acceptable_chars(str2, acceptable_chars))
The given string is: 1234654185
Checking if it contains only {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
True
The given string is: 12346@
Checking if it contains only {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
False

Using Regular Expressions

The re.fullmatch() function from the re module checks if the entire string matches a specific pattern of allowed characters. If the whole string matches the pattern, all characters are valid ?

Example: Valid Characters Only

import re

str1 = "abcdabcd"
print("The given string is:", str1)
print("Contains only a-d:", bool(re.fullmatch(r"[abcd]+", str1)))
The given string is: abcdabcd
Contains only a-d: True

Example: Invalid Character Present

import re

str1 = "abcde"
print("The given string is:", str1)
print("Contains only a-d:", bool(re.fullmatch(r"[abcd]+", str1)))
The given string is: abcde
Contains only a-d: False

Using Character Validation with all()

You can iterate through each character in the string and check if it exists in a predefined list of allowed characters. The all() function ensures every character passes validation ?

acceptable_chars = ['a', 'b', 'c', 'd']
str1 = "abcabcd"

print("The given string is:", str1)
validation = all(char in acceptable_chars for char in str1)
print("Contains only allowed characters:", validation)

# Test with invalid character
str2 = "abcx"
print("The given string is:", str2)
validation2 = all(char in acceptable_chars for char in str2)
print("Contains only allowed characters:", validation2)
The given string is: abcabcd
Contains only allowed characters: True
The given string is: abcx
Contains only allowed characters: False

Comparison

Method Performance Best For
Set Comparison Fast Large character sets
Regular Expressions Moderate Complex patterns
Character Validation Slow Simple validation logic

Conclusion

Use set comparison for the fastest performance with large character sets. Regular expressions work well for complex patterns, while character validation with all() offers simple, readable code for basic validation needs.

Updated on: 2026-03-24T16:32:21+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements