Python String isspace() Method



The python string isspace() method is used to check whether the string consists of white space characters. This method returns true if all the characters in the input string are white space characters and there is at least one character. Otherwise, it returns false.

A character is a white space if in the Unicode character database, either its general category is Zs (“Separator, space”), or its bidirectional class is one of WS, B, or S.

Let us look into more details of this method in the following section.

Syntax

Following is the syntax for the python string isspace() method −

str.isspace()

Parameters

The python string isspace() method does not contain any parameters.

Return Value

This method returns true if all characters in the string are white space characters and there is at least one character and false otherwise.

Example

A normal empty space " " is a white space character.

The following is an example of the python string isspace() method.

str = " "
result=str.isspace()
print("Are all the characters of the string spaces?", result)

On executing the above program, the following output is generated -

Are all the characters of the string spaces? True

Example

The alphabets do not come under white space characters. In the following example we are creating a string "s" with alphabetical characters and testing using this method.

str = "s"
result=str.isspace()
print("Are all the characters of the string spaces?", result)

The following is the output obtained by executing the above program -

Are all the characters of the string spaces? False

Example

The horizontal tab space '\t' is a white space character. Let us test this using an example.

str = "\t\t\t"
result=str.isspace()
print("Are all the characters of the string spaces?", result)

The following output is obtained by executing the above program -

Are all the characters of the string spaces? True

Example

The newline '\n' is also considered as a white space character. In the following is example we are creating a string holding the new line character and verifying whether it is a white space.

str = "\n"
result=str.isspace()
print("Are all the characters of the string spaces?", result)

The above program, on executing, displays the following output -

Are all the characters of the string spaces? True

Example

The vertical tab('\v'), feed('\f'), and carriage return('\r') are considered as white space characters.

str = "\v\f\r"
result=str.isspace()
print("Are all the characters of the string spaces?", result)

The output of the above program is displayed as follows -

Are all the characters of the string spaces? True
python_strings.htm
Advertisements