Python String isspace()

The Python isspace() string method is used to check if a given string contains only whitespace characters or not.

  • If all characters of a string are space then this method returns true. Otherwise, it returns false.

  • Whitespace characters are as follows:

    • ' ' - Indicates Space

    • '\t' - Indicates Horizontal tab

    • '\n' - Indicates Newline

    • '\v' - Indicates Vertical tab

    • '\f' - Indicates Feed

    • '\r' - Indicates Carriage return

You must be wondering where on earth I can use this function, but if you are here from a Google search then you already have a usecase for this function, if not well, you can use it to check if a string is empty or not, if all a string has spaces then it is more or less an empty string.

Python String isspace(): Syntax

Below we have a basic syntax of isspace() string method in Python:


In the above syntax, the string is used to denote the string variable which is used to check if it contains all whitespace characters or not.

Python String isspace(): Parameters

This method does not contain any parameters and it is also clear from the above syntax.

If any parameter is passed, then this method throws an exception.

Python String isspace(): Returned Values

This method returns true if all the characters of a string are whitespaces and false in all other cases.

Python String isspace(): Basic Example

Below we have an example to show the working of String isspace() method:

str1 = "  "
str2 = "Hello    Boy"
str3 = "\n\n\t\n\n"
str4 = "HelloGirl"

The Output of the above code will be:


Python String isspace(): To calculate the occurrence of whitespace in a string

Let us see the code snippet below which is helpful in checking the occurrence of whitespace in a string:

string = 'Titanic movie starring\n\n\n\n\n\n john'
count = 0
for a in string:
    if (a.isspace()) == True:
        count += 1

The Output will be:


Time for Live Example!

Now it is time for a Live Example where we see different ways to use isspace() method:


In this tutorial, we saw that to check if a given string only contains whitespace or not the isspace() method of strings in Python is used.