Python’s string data type is immutable, meaning you can not change a string once defined. For example, a string with spaces is an empty string with a non-zero size.
When you check the empty string with len() or not operator, it counts space as a character in the string; 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,
- Using not operator
- By using len() function
- Using not + string.strip()
- By using not + string.isspace()
Method 1: Checking empty string using not operator
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")