96 of 151 menu

The isspace method

The isspace method checks if a string contains only spaces. In addition to spaces, the method recognizes the following characters - '\t' (horizontal indent), '\n' (new line), '\v' (vertical indent), '\f' (move down to the next page), '\r' (return to the beginning of the line). Nothing is passed to the method parameters. The method returns the boolean values ​​True or False.

Syntax

string.isspace()

Example

Let's check that our string consists only of whitespace characters:

txt = ' ' print(txt.isspace())

Result of code execution:

True

Example

Now let's say there are other symbols in the line:

txt = 'abc de' print(txt.isspace())

As a result, we will see an error:

False

Example

Let's check if the character '\n' is present in the string:

txt = '\n' print(txt.isspace())

Result of code execution:

True

See also

  • method isnumeric,
    which checks if a string contains only numbers
  • method isalnum,
    which checks if a string contains letters and numbers
  • method isalpha,
    which checks if a string contains only letters
  • method isdigit,
    which checks if a string contains only numbers
byenru