In this tutorial, we will look at how to check if a string contains only whitespace characters in Python with the help of examples.
How to check if a string contains only whitespace characters?
You can use the string isspace()
function to check if all the characters in a string are whitespace characters or not in Python. The following the syntax –
# check if string contains only whitespace s.isspace()
It returns True
if all the characters in the string are whitespace characters. It returns False
if any of the characters in the string is not a whitespace character or if it’s applied on an empty string.
Let’s look at an example –
# create a string s = " " # check if string contains only whitespace characters print(s.isspace())
Output:
True
We get True
as the output as the string s contains only whitespace characters.
Note that whitespace characters in Python are not just limited to a single space, ' '
. Characters such as the tab space character, '\t'
the newline character, '\n'
etc. are also considered whitespace characters.
Let’s look at another example. This time with a string containing characters like '\t'
, '\n'
, etc.
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.
# create a string s = " \n \t\t\n" # check if string contains only whitespace characters print(s.isspace())
Output:
True
We again get True
as the output. This is because '\t'
and '\n'
are also considered whitespace characters and there are no non-whitespace characters in the string s.
Now, let’s look at an example of a string containing some characters other than whitespace characters.
# create a string s = " abc" # check if string contains only whitespace characters print(s.isspace())
Output:
False
We get False
as the output.
You might also be interested in –
- Python – Check If String Contains Only Letters
- Python – Check if String Contains Only Numbers
- Check If String Contains Only Certain Characters in Python
- Python – Check If String Contains Only Letters and Numbers
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.