Python program to check if a string is empty or not

| | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Let’s take a look at various methods of checking if a string is empty or not.

Method # 1: Using len()

Using len () is the most common method for validating a zero-length string. Although it ignores the fact that a line with only spaces should also be considered practically an empty line, even if it is not zero.

# Python3 demo code
# check if the line is empty
# using len ()


# initialization string

test_str1 = ""

test_str2 = " "


# check if the line is empty

print ( "The zero length string without spaces is empty? : " , end = " ")

if ( len (test_str1) = = 0 ):

print ( " Yes " )

else :

print ( "No" )


# prints no

print ( "The zero length string with just spaces is empty? : " , end = " ")

if ( len (test_str2) = = 0 ):

print ( " Yes " )

else :

print ( "No" )

Output:

 The zero length string without spaces is empty?: Yes The zero length string with just spaces is empty?: No 

Method # 2: Using not

The not statement can also perform a task similar to len () and checks a string of length 0, but as above, it assumes that a string containing only spaces is also non-empty, which practically shouldn’t be true.

# Python3 demo code
# check if the line is empty
# not use


# initialization string

test_str1 = " "

test_str2 = " "


# check, e If the string is empty

print ( "The zero length string without spaces is empty? : " , end = " ")

if ( not test_str1):

print ( "Yes" )

else :

print ( "No" )


# no prints

print ( "The zero length string with just spaces is empty?:" , end = "")

if ( not test_str2):

print ( "Yes" )

else :

print ( "No" )

Output:

 The zero length string without spaces is empty? : Yes The zero length string with just spaces is empty? : No 

Method # 3: Using not + str.strip()

Problem empty string + zero length could possibly be eliminated with strip () , strip () returns true if it encounters spaces, hence checking for that might fix the problem checks for a purely blank line.

# Python3 demo code
# check if the line is empty
# using not + strip ()


# initializing string

test_str1 = ""

test_str2 = " "


# check if the line is empty

print ( "The zero length string without spaces is empty? : " , end = " ")

if ( not (test_str1 and test_str1.strip ())):

print ( "Yes" )

else :

print ( " No " )


# prints yes

print ( " The zero length string with just spaces is empty?: " , end = "")

if ( not (test_str2 and test_str2.strip ())):

print ( "Yes" )

else :

print ( " No " )

Exit :

 The zero length string without spaces is empty? : Yes The zero length string with just spaces is empty? : Yes 

Method # 4: Using not + str.isspace

Works similarly to the above method and checks for spaces in the string. This method is more efficient because strip () requires a strip operation, which takes the computational load if not. spaces are a good number.

# Python 3 demo code
# check if the line is empty
# using not + isspace ()


# initializing string

test_str1 = ""

test_str2 = " "


# check if the string is empty

print ( "The zero length string without spaces is empty?: " , end = " ")

if ( not (test_str1 and not test_str1.isspace ( ))):

print ( "Yes" )

else :

print ( "No" )


# prints yes

print ( "The zero length string with just spaces is empty? : " , end = " ")

if ( not (test_str2 and not test_str2.isspace ())):

print ( "Yes" )

else :

print ( " No " )

Output:

 The zero length string without spaces is empty?: Yes The zero length string with just spaces is empty?: Yes 

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python program to check if a string is empty or not, check other Counters-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Schneider Ungerschaft

Prague | 2022-11-30

Simply put and clear. Thank you for sharing. Python program to check if a string is empty or not and other issues with isspace was always my weak point 😁. Will use it in my bachelor thesis

Oliver Zelotti

Tallinn | 2022-11-30

Maybe there are another answers? What Python program to check if a string is empty or not exactly means?. Will get back tomorrow with feedback

Angelo Williams

Vigrinia | 2022-11-30

Simply put and clear. Thank you for sharing. Python program to check if a string is empty or not and other issues with Counters was always my weak point 😁. Will use it in my bachelor thesis

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically