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!")
Do comment if you have another example or doubts on this Python string topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.