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 sorted()
Python String index()
Python Program to Add Two Numbers
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Exception Handling Using try, except and finally statement
Python String count()
Python Custom Exceptions
Python Program to Capitalize the First Character of a String
Python Program to Find LCM
Python Dictionary pop()
Python Dictionary keys()
Python bool()
Python Program to Check the File Size
Python Data Structures and Algorithms - Benjamin Baka
Python Dictionary
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python List remove()
Converting String to Stream of chars
Python Program to Find the Factors of a Number
Python RegEx
Python Set isdisjoint()
Python Set intersection()
Python list()
Python exec()
Python String rindex()
Python String partition()
Python Program to Convert String to Datetime
Python Errors and Built-in Exceptions
Python Program to Parse a String to a Float or Int
Python Program to Check if a Number is Odd or Even
Python Tuple