EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Python String Tutorial Python Test Empty String
 

Python Test Empty String

Updated April 15, 2023

Python Test Empty String

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Python Test Empty String-1.1

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:

Python Test Empty String-1.2

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:

Python Test Empty String-1.3

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:

Python Test Empty String-1.4

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:

Python Test Empty String-1.5

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:

Output-1.6

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:

Output-1.7

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:

Output-1.8

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.

  1. Python Doubly Linked List
  2. Deque in Python
  3. Linear Search in Python
  4. Shell sort in Python

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW