
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
- Python Useful Resources
- Python - Questions and Answers
- Python - Quick Guide
- Python - Tools/Utilities
- Python - Useful Resources
- Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
#!/usr/bin/python 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.
#!/usr/bin/python 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.
#!/usr/bin/python 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.
#!/usr/bin/python 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.
#!/usr/bin/python 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