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

Basic tutorial of Python

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Usage and example of Python string isspace()

String methods of Python

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()

The isspace() parameter

The isspace() method does not take any parameters.

The return value of isspace()

The 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 operation of isspace()

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

The output when running the program 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')

The output when running the program is:

All space characters
Contains non-space characters

String methods of Python