Python string Empty or Whitespace | Example code

Using isspace() method you can get the knowledge about strings to have only space or not and for Empty strings, you can get them by using “not” in if-else.

Example string empty or whitespace in python

Return True if there are only whitespace characters in the string and there is at least one character, false otherwise.

# string with space
str1 = " "

# string without space (empty string.
str2 = ""

print("str1 space: " + str(str1.isspace()))
if not str1:
    print("str1 Empty String!")

print("str2 space: " + str(str2.isspace()))
if not str2:
    print("str1 Empty String!")


Python string Empty or Whitespace

Do comment if you have another example or doubts on this Python string topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.