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:
Trueif all characters in the string are whitespace charactersFalseif 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 dict()
Python list()
Python tuple()
Python Package
Python Program to Find ASCII Value of Character
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Format ZonedDateTime to String
Generate a String
Python pow()
Python if...else Statement
Python Set discard()
Python String isdecimal()
Convert String to int or Integer in Java
Python help()
Python Numbers, Type Conversion and Mathematics
Python String isalpha()
Python Variables, Constants and Literals
Python String index()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python repr()
Encode a String to UTF-8 in Java
Python Program to Catch Multiple Exceptions in One Line
Python bytearray()
Converting a List to String in Java
Python Modules
Python int()
Python Shallow Copy and Deep Copy
Python Program to Trim Whitespace From a String
Python List clear()
Python String title()
Python Program to Differentiate Between type() and isinstance()
Python String endswith()