DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

Check if string is empty or not in Python

ItsMyCode |

In this article, you will learn how to check if string is empty or not using different approaches with examples.

Python program to check if string is empty or not

In Python, strings are an array of bytes representing the Unicode characters. Strings are immutable in Python, which means an immutable object is one that, once created, will not change in its lifetime.

There are various methods to check if a string is empty or not in Python. We will discuss the top 4 approaches in this article.

  1. Using len() method
  2. Using not operator
  3. Using not+str.strip() method
  4. Using not + str.isspace method

Using len() funtion

The len() method is a built-in function in Python that returns the length of a string. We can use this technique to determine if the string is empty or not.

If the len() method returns 0, it means the string is empty. Otherwise, it’s not empty.

Example – Check if the string is empty or not using the len() method.

The string with empty spaces is actually an empty string, but the len() method treats whitespaces as Unicode characters and returns the length of the string.

In the below example, we determine if the string is empty or not by checking if the length of the string is equal to 0 using the “if” statement.

# Check if the string is empty or not using len() method

text1 = ""
text2 = " "
text3 = "Hello World"

print("Length of text1 :", len(text1))
print("Length of text2 :", len(text2))
print("Length of text3 :", len(text3))

if(len(text1) == 0):
    print("String is empty")
else:
    print("String is not empty")

if(len(text2) == 0):
    print("String is empty")
else:
    print("String is not empty")

if(len(text3) == 0):
    print("String is empty")
else:
    print("String is not empty")

Enter fullscreen mode Exit fullscreen mode

Output

Length of text1 : 0
Length of text2 : 5
Length of text3 : 11
String is empty
String is not empty
String is not empty
Enter fullscreen mode Exit fullscreen mode

Using not operator

The not operator can also perform similar to len() method and check if the length of the string is 0 or not internally.

Again, the not operator considers the whitespaces a non-empty string similar to the len() method, which is invalid.

Example – Check if the string is empty or not using not operator

# Check if the string is empty or not using not operator

text1 = ""
text2 = " "
text3 = "Hello World"

if(not text1):
    print("String is empty")
else:
    print("String is not empty")

if(not text2):
    print("String is empty")
else:
    print("String is not empty")

if(not text3):
    print("String is empty")
else:
    print("String is not empty")

Enter fullscreen mode Exit fullscreen mode

Output

String is empty
String is not empty
String is not empty
Enter fullscreen mode Exit fullscreen mode

Using not+str.strip() method

In the above method, the strings with whitespaces are considered non-empty strings, and we can overcome this issue by using the strip() method, which truncates the whitespaces at both leading and trailing ends.

In the below example, the strip()method returns true if it encounters whitespaces, thus solving the issue.

# Check if the string is empty or not using not operator and strip() method

text1 = ""
text2 = " "
text3 = "Hello World"

if(not (text1 and text1.strip())):
    print("String is empty")
else:
    print("String is not empty")

if(not (text2 and text2.strip())):
    print("String is empty")
else:
    print("String is not empty")

if(not (text3 and text3.strip())):
    print("String is empty")
else:
    print("String is not empty")

Enter fullscreen mode Exit fullscreen mode

Output

String is empty
String is empty
String is not empty

Enter fullscreen mode Exit fullscreen mode

Using not + str.isspace method

The most efficient way is to use a combination of not and str.isspace() method because the strip() method has to remove the whitespaces, and it’s a costly operation when compared to the issapce()method.

# Check if the string is empty or not using not operator and isspace() method

text1 = ""
text2 = " "
text3 = "Hello World"

if(not (text1 and not text1.isspace())):
    print("String is empty")
else:
    print("String is not empty")

if(not (text2 and not text2.isspace())):
    print("String is empty")
else:
    print("String is not empty")

if(not (text3 and not text3.isspace())):
    print("String is empty")
else:
    print("String is not empty")

Enter fullscreen mode Exit fullscreen mode

Output

String is empty
String is empty
String is not empty
Enter fullscreen mode Exit fullscreen mode

The post Check if string is empty or not in Python appeared first on ItsMyCode.

Top comments (0)