English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Python Functions

Python Data Types

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python string isspace() usage and example

Python string methods

If the string contains only space characters, the isspace() method will return True. Otherwise, it returns False.

Characters used for spacing are called whitespace characters. For example: tab, space, newline, etc.

The syntax of isspace() is:

string.isspace()

isspace() parameters

isspace() method without any parameters.

isspace() return value

isspace() method returns:

  • True if all characters in the string are space characters

  • False if the string is empty or contains at least one non-Printable() characters

Example1:The working of isspace()

s = '  \t'
print(s.isspace())
s = ' a '
print(s.isspace())
s = ''
print(s.isspace())

When running the program, the output is:

True
False
False

Example2:How to use isspace()?

s = '\t	\n'
if s.isspace() == True:
  print('All space characters')
else:
  print('Contains non-space characters')
  
s = '2+2 = 4'
if s.isspace() == True:
  print('All space characters')
else:
  print('Contains non-space characters')

When running the program, the output is:

All space characters
Contains non-space characters

Python string methods