Python String isspace Method

The isspace function in Python serves the purpose of verifying whether a string consists solely of whitespace characters. It yields a true value if the string contains only whitespace; conversely, it will return false if there are any non-whitespace characters present. Whitespace characters include spaces, newlines, and tabs, among others, and are categorized in the Unicode character database as either Other or Separator.

Syntax of Python String isspace Method

It has the following syntax:

Example

isspace()

Parameters

No parameter is required.

Return

It returns either True or False.

Different Examples for Python String isspace Method

Let us examine a few instances of the isspace method to grasp its functionalities more thoroughly.

Example 1

Let’s consider an example to illustrate how the Python String isspace method operates.

Example

# Python isspace() method example

# Variable declaration

str = " " # empty string

# Calling function

str2 = str.isspace()

# Displaying result

print(str2)

Output:

Example 2

To illustrate the functionality of the Python String isspace method, let us consider an example.

Example

# Python isspace() method example

# Variable declaration

str = "ab cd ef" # string contains spaces

# Calling function

str2 = str.isspace()

# Displaying result

print(str2)

Output:

Example 3

isspace method returns true for all whitespaces like:

  • ' ' - Space
  • '\t' - Horizontal tab
  • '\n' - Newline
  • '\v' - Vertical tab
  • '\f' - Feed
  • '\r' - Carriage return
  • Example
    
    # Python isspace() method example
    
    # Variable declaration
    
    str = " " # string contains space
    
    if str.isspace() == True:
    
        print("It contains space")
    
    else:
    
        print("Not space")
    
    str = "ab cd ef \n"
    
    if str.isspace() == True:
    
        print("It contains space")
    
    else:
    
        print("Not space")
    
    str = "\t \r \n"
    
    if str.isspace() == True:
    
        print("It contains space")
    
    else:
    
        print("Not space")
    

Output:

Output

It contains space

Not space

It contains space

Input Required

This code uses input(). Please provide values below: