Skip to content

Python string Empty or Whitespace | Example code

  • by

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!")

Output:

Python string Empty or Whitespace

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

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

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

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