Python String isspace() Method with Examples

In the previous article, we have discussed Python String isprintable() Method Examples
isspace() Method in Python:

If the string contains only whitespace characters, the isspace() method returns True. If it does not, it returns False.

Whitespace characters are characters that are used for spacing. For example, tabs, spaces, newlines, and so on.

Syntax:

string.isspace()

Parameters: This function has no parameters.

Return Value:

The isspace() method gives:

  • True if the string contains only whitespace characters.
  • If the string is empty or contains at least one non-printable character, it returns False.

Examples:

Example1:

Input:

Given string = ' Python-programs '
Given string = ' \t'
Given string = ''

Output:

The given string =    Python-programs  
Checking if the given string is has whitespace or not =  False
The given string =      	
Checking if the given string is whitespace or not =  True
The given string =  
Checking if the given string is whitespace or not =  False

Example2:

Input:

Given string = 'welcome to python programs'
Given string = '\t \t'
Given string = 'hellobtechgeeks'

Output:

The given string =  welcome to python programs
Checking if the given string is has whitespace or not =  False
The given string =  	  	
Checking if the given string is whitespace or not =  True
The given string =  hellobtechgeeks
Checking if the given string is whitespace or not =  False

String isspace() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply isspace() function to the given string in which if the string contains only whitespace characters, the isspace() method returns True. If it does not, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has only whitespace characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = '  Python-programs  '
# Apply isspace() function to the given string in which if the string
# contains only whitespace characters, the isspace() method returns True.
# If it does not, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isspace()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has only whitespace
# characters or not.
print("Checking if the given string is has whitespace or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = '    \t'
rslt_2 = gvn_strng2.isspace()
print("The given string = ", gvn_strng2)
print("Checking if the given string is whitespace or not = ", rslt_2)

gvn_strng3 = ''
rslt_3 = gvn_strng3.isspace()
print("The given string = ", gvn_strng3)
print("Checking if the given string is whitespace or not = ", rslt_3)

Output:

The given string =    Python-programs  
Checking if the given string is has whitespace or not =  False
The given string =      	
Checking if the given string is whitespace or not =  True
The given string =  
Checking if the given string is whitespace or not =  False

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.