Python String isspace Method - Python Tutorial | Logic Practice
Python Course / Data Types / Python String isspace Method

Python String isspace Method

BLUF: This lesson on Python String isspace Method provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python String isspace Method

Mastering Python String isspace Method is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience