Home Python How to Check If The String Is Empty Or Not

How to Check If The String Is Empty Or Not

0
How to Check If  The String Is Empty Or Not
Check If The String Is Empty Or Not

Everyone knows that strings data types are immutable in Python & hence have more complex handling when talking about its operations.

You can’t change a string once defined.

Important note: that is The string with spaces is actually empty but has non-zero size.

In this post, we will learn how to check if the string is empty with the four methods for you.

Let’s begin!

Contents

How to check if the string is empty

  1. len() function
  2. not operator
  3. not + string.strip()
  4. not + string.isspace()

What does the len() function do?

The len() function is very common and easy to check for the zero-length strings in python. Even if it ignores fact that a string with spaces also should be practically considered an empty string it’s non-zero.

How to use len() to check strings?

  • First of all, make two empty variables. Now there you are going to check is it none?
  • Then simply, you can use the if and else statement.
  • Let’s have a quick idea in the following code.

whole_blogs1 = ""
whole_blogs2 = ""

# checking if string is empty
print ("Check If The String Is Empty ? : ", end = "")
if(len(whole_blogs1) == 0):
	print ("Yes")
else :
	print ("No")

# prints No
print ("Check If The String Is Empty? : ", end = "")
if(len(whole_blogs2) == 0):
	print ("Yes")
else :
	print ("No")

Output:

Check If The String Is Empty ? : Yes

Check If The String Is Empty? : Yes

  • As we passed both empty variables in the if and else statement and we put “Yes” and “No” in the print function for checking.
  • As you can see in the above line code.

In a second way, we are going to discuss not operator which will show the exact thing.

How to use not operator?

It is not similar to len() nor performs the same thing. It starts checking from 0 length string. Not operator checks just spaces and empty variables. It does not consider to check as blank. There is no chance of True and False statements as well.

Here are going to disclose the code following.


whole_blogs1 = ""
whole_blogs2 = ""

# checking if string is empty
print ("Check If The String Is Empty ? : ", end = "")
if(not whole_blogs1):
	print ("Yes")
else :
	print ("No")

# prints No
print ("Check If The String Is Empty ? : ", end = "")
if(not whole_blogs2):
	print ("Yes")
else :
	print ("No")

Output:

Check If The String Is Empty ? : Yes

Check If The String Is Empty ? : Yes

Using not + string.strip()

In Python, sometimes you can’t get the space that exists in the strings which may not be treated as blank strings.

So if you want to get an empty string you can use this function.

Let’s see in the code.


whole_blogs1 = ""
whole_blogs2 = ""

# checking if string is empty
print ("Is it Yes? : ", end = "")
if(not (whole_blogs1 and whole_blogs1.strip())):
	print ("Yes")
else :
	print ("No")

# prints Yes
print ("Is it Yes? : ", end = "")
if(not(whole_blogs2 and whole_blogs2.strip())):
	print ("Yes")
else :
	print ("No")

Output:
Is it Yes? : Yes

Is it Yes? : Yes

Using not + string.isspace()

You can also use the above guideline because it is similar to not + str.strip(). This function is pretty easy because strip() requires performing the strip operation.

It takes computation loads if not spaces are a good number.



# using not + isspace()

# initializing string
whole_blogs1 = ""
whole_blogs2 = "  "

# checking if string is empty
print ("Let's see? : ", end = "")
if(not (whole_blogs1 and not whole_blogs1.isspace())):
	print ("Yes")
else :
	print ("No")

# prints Yes
print ("Let's see? : ", end = "")
if(not (whole_blogs2 and not whole_blogs2.isspace())):
	print ("Yes")
else :
	print ("No")

Output:

Let’s see? : Yes

Let’s see? : Yes

Read More: How to check if the list is empty Python

LEAVE A REPLY

Please enter your comment!
Please enter your name here