Check if string is empty or not in Python
Tags: python, programming, codenewbie, tutorial
Published: 2021-12-13 16:14:34

---
title: Check if string is empty or not in Python
published: true
date: 2021-12-12 09:13:29 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/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](https://www.w3basic.com/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.

```python 
# 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")

```

**Output**

```python 
Length of text1 : 0
Length of text2 : 5
Length of text3 : 11
String is empty
String is not empty
String is not empty
```

## 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**

```python 
# 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")

```

**Output**

```python 
String is empty
String is not empty
String is not empty
```

## 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. 

```python 
# 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")

```

**Output**

```python 
String is empty
String is empty
String is not empty

```

##  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.

```python 
# 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")

```

**Output**

```python 
String is empty
String is empty
String is not empty
```

The post [Check if string is empty or not in Python](https://itsmycode.com/check-if-string-is-empty-or-not-in-python/) appeared first on [ItsMyCode](https://itsmycode.com).