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 all()
Encode a String to UTF-8 in Java
Python Program to Create a Countdown Timer
Python for Loop
Python bytearray()
Python strftime()
Python Exception Handling Using try, except and finally statement
Converting a Stack Trace to a String in Java
Python Program to Remove Duplicate Element From a List
Python Tuple index()
Python Program to Generate a Random Number
Python List pop()
Python abs()
Check If a String Is Numeric in Java
Python sorted()
Python Program to Split a List Into Evenly Sized Chunks
Python __import__()
Python String splitlines()
Python Program to Print the Fibonacci sequence
Converting String to Stream of chars
Python Directory and Files Management
Python Tuple
Python Program to Make a Simple Calculator
Python Program to Find Sum of Natural Numbers Using Recursion
Python String rjust()
Python String partition()
Python Program to Check if a Number is Positive, Negative or 0
Python compile()
Python Program to Convert Bytes to a String
Python String isdigit()
Python Program to Print all Prime Numbers in an Interval
Python any()