close

How to check string is empty in Python

In python the string data type can not change once you defined it. It means it is immutable in python. If the string has spaces than it is empty string but with non zero size. You can check the string in python is empty or not. Here we are learning about of it. So lets learn How to check string is empty in Python

How to check string is empty in Python

  1. check string is empty in Python

    to check string is empty in Python just Use not operator. You can use not operator to check the string is empty or not. if the string have white spaces then it consider as a non empty string and have no white space than its a empty string. lets see this:
    mystr = '' if not mystr: print("The string is empty") else: print("The string is not empty") Output: The string is empty Here is the example which is non empty string. mystr = 'sunday' if not mystr: print("The string is empty") else: print("The string is not empty") Output: The string is not empty

  2. How to check string is empty in Python

    to check string is empty in Python just use len(). By using len() function you can check the string is empty or not. let learn this by given example:
    mystr = "" print("Check if the string is empty : ", end="") if(len(mystr)): print("my string is not empty") else: print("my string is empty")
    Output: Check if the string is empty : my string is empty
    another example of this method is here: mystr = "sunday" print("Check if the string is empty : ", end="") if(len(mystr)): print("my string is not empty") else: print("my string is empty") Output: Check if the string is empty : my string is not empty

Method 1: Use not operator

You can use not operator to check the string is empty or not. if the string have white spaces then it consider as a non empty string and have no white space than its a empty string. lets see this:

mystr = ''
if not mystr:
    print("The string is empty")
else:
    print("The string is not empty")

Output:

The string is empty

Here is the example which is non empty string.

mystr = 'sunday'
if not mystr:
    print("The string is empty")
else:
    print("The string is not empty")

Output:

The string is not empty

Method 2: use len() function

By using len() function you can check the string is empty or not. let learn this by given example:

mystr = ""
print("Check if the string is empty : ", end="")
if(len(mystr)):
    print("my string is not empty")
else:
    print("my string is empty")

Output:

Check if the string is empty : my string is empty

another example of this method is here:

mystr = "sunday"
print("Check if the string is empty : ", end="")
if(len(mystr)):
    print("my string is not empty")
else:
    print("my string is empty")

Output:

Check if the string is empty : my string is not empty

Method 3: Use strip() method

Strip () method can be also use to check if the string is empty or not. By using this method you can check if the string is empty or else it have black spaces or it is not empty. You can easily use this method and check the string is empty or not. Lets learn this by given below example:

def check_for_Blanks(myStr):
    if myStr and myStr.strip(): 
        return False    
    return True 
print(check_for_Blanks('monday  '))
print(check_for_Blanks(''))
print(check_for_Blanks(None))
print(check_for_Blanks('  '))

Output :

False
True
True
True

Method 4 : Use not + str.isspace 

This is use for checking if the string is empty or not. By using not operator and str.isspace you can check the string is contain the white space in it or not. So lets learn about of this by given below example.

mystr1 = ""
mystr2 = "  "
print ("The string without spaces is empty ? : ", end = "")
if(not (mystr1 and not mystr1.isspace())):
    print ("Yes")
else :
    print ("No")

print ("The string with just spaces is empty ? : ", end = "")
if(not (mystr2 and not mystr2.isspace())):
    print ("Yes")
else :
    print ("No")

Output :

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

Conlusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment