EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
PYTHON Course Bundle - 81 Courses in 1 | 59 Mock Tests
 257+ Hours of HD Videos
81 Courses
59 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.8
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more