Cheap Jerseys Cheap NFL Jerseys Cheap NFL Jerseys Cheap Football jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap NHL Jerseys Cheap Jerseys From China Cheap MLB Jerseys Cheap Jerseys Cheap Jerseys Cheap Jerseys Wholesale Jerseys Python String isspace() Method - WTMatter

Python String isspace() Method

Python String Isspace() Method

This is a tutorial of the Python String isspace() method. Learn to check if a given string only contains whitespace characters or not with examples.

Python String isspace()

The str.isspace() method returns True either if the given string contains only whitespace characters and if not it returns False.

Syntax

Given below is the syntax of the str.isspace() method.

isspace() Parameters

This method does not take any arguments as it only checks the characters in the string on which it is applied, so no additional data is required in the form of arguments.

isspace() Return Value

It returns the boolean value True or False according to the following logic.

  • Returns True if all of the characters in the given string are only whitespaces.
  • Returns False if not all the characters in the given string are whitespace characters or the string is empty.

Examples

Given below are the two examples demonstrating the usage of the Python String isspace() method.

Example 1. Checking if a given String contains all the whitespaces or not using str.isspace() method

In this example, we’re applying the method isspace() on several different strings to check the result, for what strings it returns True and for what it returns False.

#single space with other characters
name = "Gurmeet Singh"
print(name.isspace())


#Single Letter with multiple whitespaces
letter = "   G   "
print(letter.isspace())


#Empty String
empty = ""
print(empty.isspace())

#Only Single Whitespace
single = " "
print(single.isspace())

#Multiple Whitespaces
multiple = "     "
print(multiple.isspace())

Output.

False
False
False
True
True

Example 2. Using the Python String isspace() method with if-else statement

In the following example, we’ve directly used the str.isspace() method as the condition for the if-else statement to print a string accordingly after checking if the given string only contains whitespace characters or not.

#Using str.isspace() with if-else statement

#Given String
name = "             "

if name.isspace():
    print("name only contains whitespace characters.")
else:
    print("name also contains characters other than whitespaces characters.")

Output.

name only contains whitespace characters.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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