Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

4 Methods to Check Whether a String Is Empty in Python

  • Adrita DasAdrita Das
  • February 27, 2022
  • Python Programming Examples
4 Methods to Check Whether a String Is Empty in Python

The title sounds a bit odd right as one might think we can simply check whether spring is empty or not with the help of the len() or not operator. But we need to remember here that it will space as a character in the string and will show the string as a  non-empty string. In this article, we will learn the methods we can use to check whether a string is empty or not. Let’s begin!

Methods to Check Whether a String Is Empty in Python

Let’s look at 4 different methods to check if a string is empty or not in Python. We’ll explore each method with an example and show the output to help you perform the same on your own.

1. Using NOT operator

This method considers a string with spaces as a non-empty string. It counts the space in the string as a character. We should know that string with space is an empty string and has a non-zero size but this method ignores that fact. 

For example

str1 = ""
str2 = "  "
 
if(not str1):
    print ("Yes the string is empty")
else :
    print ("No the string is not empty")
 
if(not str2):
    print ("Yes the string is empty")
else :
    print ("No the string is not empty"

Output:

Yes the string is empty
No the string is not empty

You can see it printed the string with space in it as a non-empty string.

2. Using len() function

Like the not operator, this also considers a string with space as a non-empty string. This method checks for non-empty zero-length strings.

For example :

str1 = ""
str2 = "  "
 

if(len(str1) == 0):
    print ("Yes the string is empty ")
else :
    print ("No the string is not empty")
 

if(len(str2) == 0):
    print ("Yes the strinf is empty")
else :
    print ("No the string is not empty")

Output:

Yes the string is empty 
No the string is not empty

3. Using the not+str.strip () method

This method does not ignore the fact of an empty+non-zero-length string. Therefore, this method can serve the purpose of checking for an empty zero-length string. It looks for an empty, non-zero-length string.

For example:

str1 = ""
str2 = "  "
 
if(not (str1 and str1.strip())):
    print ("Yes the string is empty")
else :
    print ("No the string is not empty")
 
if(not(str2 and str2.strip())):
    print ("Yes the string is empty")
else :
    print ("No the string is not empty")

Output:

Yes the string is empty
Yes the string is empty

4. Using not str.isspace method

This method is similar to the above method. This method is considered to be more robust as it performs the strip operation which takes on the computational liabilities if the string contains a large number of spaces.

str1 = ""
str2 = "  "
 

if(not (str1 and not str1.isspace())):
    print ("Yes the string is empty")
else :
    print ("No the string is not empty")
 

if(not (str2 and not str2.isspace())):
    print ("Yes the string is empty")
else :
    print ("No the string is not empty")

Output:

Yes the string is empty
Yes the string is empty

Conclusion

So in this article, we learned quite a lot of different methods through which we can check for an empty string. Although each method has its own drawbacks, you can use them according to your suitability.

Adrita Das
Adrita Das
Articles: 24
Invert Elements of Boolean Arrays
Previous Post Invert Elements of Boolean Arrays
Next Post Numpy Random.rand() - Generate Random Data using Numpy
Numpy Random.rand() – Generate Random Data using Numpy

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.