In this tutorial, we will look at how to check if a string contains only certain characters in Python with the help of some examples.
How to check if a string contains only certain characters?
You can use a combination of the all()
function and the membership operator, in
to check if a string contains only certain characters in Python. Use the following steps –
- Create a set (or a string) containing the allowed characters.
- Iterate through the characters in the string and use the membership operator
"in"
to check if the character is in the allowed set of characters. - Use the Python built-in
all()
function to returnTrue
only if all the characters in the string are present in the allowed characters.
Let’s look at an example.
# string s = "ababaaaab" # string with only allowed characters allowed_s = "ab" # check if s contains only allowed characters print(all(ch in allowed_s for ch in s))
Output:
True
Here, we get True
as the output because all the characters in the string s
are present in the allowed characters.
Let’s look at another example.
# string s = "abcbaaaab" # string with only allowed characters allowed_s = "ab" # check if s contains only allowed characters print(all(ch in allowed_s for ch in s))
Output:
False
Here, we get False
as the output because the character “c” is not in the allowed characters.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
We can modify the allowed characters string to suit our specific use case. For example, if you want to check if the string contains only digits you can use “0123456789” as your allowed characters string.
# string s = "1150" # string with only allowed characters allowed_s = "0123456789" # check if s contains only allowed characters print(all(ch in allowed_s for ch in s))
Output:
True
Note that for the above use case, you can also just directly use the string isdigit() function.
You might also be interested in –
- Python – Check If String Contains Only Letters
- Python – Check if a String Contains Numbers
- Check if a String contains a Substring in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.