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

In this article, we are going to find out how to check if a string contains only certain characters in Python.

The first approach is by using sets. We will declare a set with all the characters that are acceptable and we will check if the input string is a subset of the acceptable characters, if it is a subset then we will print True otherwise print False.

Similar to sets in mathematics, sets are a type of data structure in Python. The order of the elements in a set is arbitrary and it could contain a variety of them. You can add and remove elements from a set, iterate over the set's components, and perform other typical operations on sets (union, intersection, difference)

## Example

In the example given below, we are taking strings as input and we are checking if the strings are made up of only specific characters using sets

def acceptableChars(str):
validation = set(str)
print("Checking if it contains only ",acceptable_chars)
if validation.issubset(acceptable_chars):
return True
else:
return False

acceptable_chars = set('0123456789')
str1 = "1234654185"
print("The given string is")
print(str1)
print(acceptableChars(str1))

str2 = "12346@"
print("The given string is")
print(str2)
print(acceptableChars(str2))


## Output

The output of the above example is given below −

The given string is
1234654185
Checking if it contains only {'7', '4', '6', '2', '3', '9', '5', '1', '0', '8'}
True
The given string is
12346@
Checking if it contains only {'7', '4', '6', '2', '3', '9', '5', '1', '0', '8'}
False


## Using Regular Expressions

The second approach is by using Regular expressions. We will use the re.match() method of the regular expressions library. In the pattern, we will give the characters that we would like to want in the string, if there are any other characters in the string, then False is returned, else True is returned.

### Example 1

In the example given below, we are taking a string as input and we are checking if it contains only [a, b, c, d] using the regular expressions

import re
str1 = "abcdabcd"

print("The given string is")
print(str1)

print("Checking if the given string contains only specific characters")


### Output

The output of the above example is given below −

The given string is
abcde
Checking if the given string contains only specific characters
False


## Using Character list

The third approach is by making a list of wanted characters and we will check if the characters present in the string belong to this list or not. If all the characters are not belonging to the character list, then False is returned, else True is returned.

### Example

In the example given below, we are taking an acceptable_chars list and we are checking in the string if the characters do belong to the acceptable_chars list or not

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

print("The given string is")
print(str1)

validation = [i in acceptable_chars for i in str1]
print("Checking if the given string contains only specific characters")
print(all(validation))


### Output

The output of the above example is as shown below −

The given string is
abcabcd
Checking if the given string contains only specific characters
True


Updated on: 07-Dec-2022

5K+ Views 