Table of Contents
The isspace() method returns True if there are only whitespace characters in the string. If not, it return False.
Characters that are used for spacing are called whitespace characters. For example: tabs, spaces, newline, etc.
The syntax of isspace()
is:
string.isspace()
1. isspace() Parameters
isspace()
method doesn’t take any parameters.
2. Return Value from isspace()
isspace()
method returns:
True
if all characters in the string are whitespace charactersFalse
if the string is empty or contains at least one non-printable character
3. Example 1: Working of isspace()
s = ' \t' print(s.isspace()) s = ' a ' print(s.isspace()) s = '' print(s.isspace())
Output
True False False
4. Example 2: How to use isspace()?
s = '\t \n' if s.isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters') s = '2+2 = 4' if s.isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters.')
Output
All whitespace characters Contains non-whitespace characters
Related posts:
Python round()
Python float()
Python Program to Illustrate Different Set Operations
Python RegEx
Python List pop()
Python Program to Print all Prime Numbers in an Interval
Python tuple()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Set symmetric_difference_update()
Python Program Read a File Line by Line Into a List
Python File I/O Operation
Python Program to Make a Simple Calculator
Python List reverse()
Python Program to Display the multiplication Table
Python String replace()
Python Program to Check Armstrong Number
Python Program to Trim Whitespace From a String
Python Program to Slice Lists
String Processing with Apache Commons Lang 3
Python Package
Python Set add()
Python Machine Learning - Sebastian Raschka
Python memoryview()
Python String split()
Python Dictionary pop()
Python Set discard()
Python Exception Handling Using try, except and finally statement
Python Global Keyword
Python Program to Differentiate Between type() and isinstance()
Python Set isdisjoint()
Python String zfill()
Python divmod()