How to Check If String is Empty or Not in Python
String data type in Python is immutable; that means you can not change a string once defined. String with spaces is actually an empty string but has non-zero size. So, when you check the empty string with len() or not operator, it counts space as a character in the string, and hence it won’t count a string with space as an empty string.
How to check an empty string in Python
To check an empty string in Python, you can use one of the following methods.
- Using not operator
- Using len() function
- Using not + string.strip()
- Using not + string.isspace()
Not operator to check empty string
The not operator in Python checks the string with just spaces to be non-empty, which should not practically be True.
# app.py strA = "" # checking if string is empty print("Check if the strA is empty : ", end="") if(not strA): print("The string is empty") else: print("No it is not empty")