Open In App

Check if String is Empty or Not - Python

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:

Using Comparison Operator(==)

The simplest and most commonly used approach is using comparison operator(==):

Python
s = ""

if s == "":
    print("The string is empty.")
else:
    print("The string is not empty.")

Output
The string is empty.

Explanation: A simple equality check (s == "") determines if the string is empty.

Using len()

The len() function returns the length of a string so if the length is 0, it means that string is empty:

Python
s = ""

if len(s) == 0:
    print("The string is empty.")
else:
    print("The string is not empty.")

Output
The string is empty.

Explanation:

  • len(s): calculates the length of s.
  • If len(s) == 0 then the string is empty.

Using Python's Truthy/Falsy Behavior

In Python, empty strings are treated as "Falsy". So, we can use this for checking if a string is empty or not.

Python
s = ""

if not s:
    print("The string is empty.")
else:
    print("The string is not empty.")

Output
The string is empty.

Explanation: "not s" negates the boolean value of s, if it's empty then it's boolean quivalent to False and its negation would be True.


Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy