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:
Map to String Conversion in Java
Python strftime()
Python Program to Get File Creation and Modification Date
Python String startswith()
Python exec()
Python frozenset()
Python time Module
Python List copy()
Python Type Conversion and Type Casting
Python isinstance()
Python Program to Remove Duplicate Element From a List
Python Multiple Inheritance
Python abs()
Format ZonedDateTime to String
Python Program to Split a List Into Evenly Sized Chunks
Python repr()
Python pass statement
Python Program to Iterate Over Dictionaries Using for Loop
Python Package
Python Shallow Copy and Deep Copy
Python Program to Display the multiplication Table
Python del Statement
Python Program to Count the Occurrence of an Item in a List
Python Dictionary fromkeys()
Python Program to Find the Sum of Natural Numbers
Python object()
Python complex()
String Processing with Apache Commons Lang 3
Python String isidentifier()
Python String swapcase()
Python Program to Create Pyramid Patterns
Python Program to Measure the Elapsed Time in Python