Python string isspace() method

Python String isspace() Method

Description

The isspace() method is used to check whether a string consists of only whitespace characters.

Syntax

The syntax of the isspace() method is as follows:

var.isspace()

Parameters

None

Return Value

This method returns True if the string contains only whitespace characters, and False if there is at least one non-whitespace character.

Example

The following example demonstrates the use of the isspace() method.

var = ""
var1 = var.isspace()
print("Original string:", var)
print("Contains only whitespace characters?:", var1)

var = " "
var2 = var.isspace()
print("Original string:", var)
print("Contains only whitespace characters?:", var2)
var = "Hello Python"
var3 = var[5].isspace()
print("Original string:", var)
print("Contains only whitespace characters?:", var3)

“” is an empty string, not a space. In “Hello Python,” character index 5 is a space.

When you run this program, it will produce the following output:

Raw string:
Contains only whitespace characters? : False
Raw string:
Contains only whitespace characters? : True
Raw string: Hello Python
Contains only whitespace characters? : True

Leave a Reply

Your email address will not be published. Required fields are marked *