
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 -
- set Comparison
- Regular Expressions
- Character Lists
These approaches help you to verify whether every character in the string belongs to a defined set of allowed characters or not.
Using Set Comparison
You can create a set of allowed characters and check if all characters in the string belong to that set or not. If the string is a subset of the allowed set, it means the string contains only valid (accepted) characters.
Example
In the following example, we define a set of allowed numeric characters and check if all characters in the given string are digits by using issubset(). If the string has any character outside the allowed set, it returns False -
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))
Following is the output obtained -
The given string is 1234654185 Checking if it contains only {'8', '2', '6', '7', '1', '9', '0', '3', '5', '4'} True The given string is 12346@ Checking if it contains only {'8', '2', '6', '7', '1', '9', '0', '3', '5', '4'} False
Using Regular Expressions
The re.fullmatch() function from the re module is used to check if the entire string matches a specific pattern of allowed characters. If the whole string matches the pattern, it means all characters are valid.
In the pattern, you list the characters that are allowed in the string. If the string contains any characters outside of this set, the match fails and False is returned; otherwise, it returns True.
Example: Only Allowed Characters
In the following example, we define a pattern that only allows the characters a, b, c, and d. Since the string consists of only these characters, it matches the pattern and returns True -
import re str1 = "abcdabcd" print("The given string is") print(str1) print("Checking if the given string contains only specific characters") print(bool(re.fullmatch(r"[abcd]+", str1)))
We get the output as shown below -
The given string is abcdabcd Checking if the given string contains only specific characters True
Example: Invalid Character Present
In the example below, the string contains the character e, which is not part of the allowed characters. So, the match fails and returns False -
import re str1 = "abcde" print("The given string is") print(str1) print("Checking if the given string contains only specific characters") print(bool(re.fullmatch(r"[abcd]+", str1)))
The output obtained is as shown below -
The given string is abcde Checking if the given string contains only specific characters False
Using Character List
You can loop through each character in the string and check if it exists in a predefined list of allowed characters. This method doesn't require sets or regular expressions.
If any character in the string is not found in the list, the function returns False; otherwise, it returns True.
Example
In the example below, we define a list of allowed characters and check if each character in the string is part of this list. The all() function ensures that every character is validated correctly -
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))
The result produced is as follows -
The given string is abcabcd Checking if the given string contains only specific characters True