How to Check If String is Empty or Not in Python
The string data type in Python is immutable; that means you can not change a string once defined. A string with spaces is 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:
- 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")