Updated April 15, 2023
Introduction to Python Test Empty String
Python is an object oriented, high level, interactive and general purpose programming language, created by Guido van Rossum in period of 1985 to 1990. Python also comes under GNU general public license, just like Perl. Python is very complex when it comes to handling of its operations and even the strings in Python are immutable. We should remember that string which has spaces is an empty string with size not equal to zero. In this article we would understand testing of Python empty string. Here we would learn how to test if a string is empty or has some characters.
Syntax of Python Test Empty String
Different method’s syntax to test if the String is Empty or not in Python:
Using ‘len()’ Method
len(Stringy_string) == 0
Using ‘not’ Method
not Stringy_string
Using ‘not + str.strip()’ Method
not (Stringy_string and Stringy_string.strip())
Using ‘not + str.isspace’ Method
not (Stringy_string and not Stringy_string.isspace())
Examples of Python Test Empty String
Testing the String, if it is Empty or not using Python explained with Examples below:
The below examples test the string if it is empty or not. If the string is empty respective output is printed, i.e. “Nothing Found, so it is EMPTY”. If the string is not empty, then the respective output is printed “Something Found, so it is NOT EMPTY” on the code execution.
1. Method 1 – Using ‘len()’
The most famous method for checking the zero-length string. However, it doesn’t consider the fact that a string that has just spaces should be considered an empty string even though it has no zero value.
Example #1
Code:
Stringy_string = ""
print ("Is the string contains anything or it is Empty? \n"
, end = "")
if(len(Stringy_string) == 0):
print ("\nNothing Found, so it is EMPTY")
else :
print ("Something Found, so it is NOT EMPTY")
print ("\nLet's move to next example now, which have something in it. \n")
Output:
Example #2
Code:
Stringy_string = "I have something"
print ("Is the string contains anything or it is Empty? \n"
, end = "")
if(len(Stringy_string) == 0):
print ("\nNothing Found, so it is EMPTY")
else :
print ("Something Found, so it is NOT EMPTY")
print ("\nLet's move to next method now, with different examples. \n")
Output:
2. Method 2 – Using ‘not’
Not operator is an alternative to len() which also checks for the strings with no value. However, it also doesnt consider the strings which has only spaces as non empty which is actually wrong.
Example #1
Code:
Stringy_string = ""
print ("Is the string contains anything or it is Empty? \n"
, end = "")
if(not Stringy_string):
print ("\nNothing Found, so it is EMPTY")
else :
print ("Something Found, so it is NOT EMPTY")
print ("\nLet's move to next example now, which have something in it. \n")
Output:
Example #2
Code:
Stringy_string = "I have something"
print ("Is the string contains anything or it is Empty? \n"
, end = "")
if(not Stringy_string):
print ("\nNothing Found, so it is EMPTY")
else :
print ("\nSomething Found, so it is NOT EMPTY")
print ("\nLet's move to next method now, with different examples. \n")
Output:
3. Method 3 – Using ‘not + str.strip()’
Example #1
Code:
Stringy_string = ""
print ("Is the string contains anything or it is Empty? \n"
, end = "")
if(not (Stringy_string and Stringy_string.strip())):
print ("\nNothing Found, so it is EMPTY")
else :
print ("Something Found, so it is NOT EMPTY")
print ("\nLet's move to next example now, which have something in it. \n")
Output:
Example #2
Code:
Stringy_string = "I have something"
print ("Is the string contains anything or it is Empty? \n"
, end = "")
if(not (Stringy_string and Stringy_string.strip())):
print ("\nNothing Found, so it is EMPTY")
else :
print ("\nSomething Found, so it is NOT EMPTY")
print ("\nLet's move to next method now, with different examples. \n")
Output:
4. Method 4 – Using ‘not + str.isspace’
Example #1
Code:
Stringy_string = ""
print ("Is the string contains anything or it is Empty? \n"
, end = "")
if(not (Stringy_string and not Stringy_string.isspace())):
print ("\nNothing Found, so it is EMPTY")
else :
print ("Something Found, so it is NOT EMPTY")
print ("\nLet's move to next example now, which have something in it. \n")
Output:
Example #2
Code:
Stringy_string = "I have something"
print ("Is the string contains anything or it is Empty? \n"
, end = "")
if(not (Stringy_string and not Stringy_string.isspace())):
print ("\nNothing Found, so it is EMPTY")
else :
print ("\nSomething Found, so it is NOT EMPTY")
print ("\nLet's move to next method now, with different examples. \n")
Output:
Conclusion
On the basis of the above article, we understood the concept of empty strings in Python. We understood how to check the strings which has zero value and how they are not able to detect strings which have just spaces through multiple examples and methods.
Recommended Articles
We hope that this EDUCBA information on “Python Test Empty String” was beneficial to you. You can view EDUCBA’s recommended articles for more information.