How to check if a Python string is Empty
In this tutorial we are going to learn about, how to check if a string is empty or not in Python.
Checking if string is empty
To check if a string is empty or not, we can use the if not
statement in Python.
Note: Empty strings are treated as falsy values in Python.
Here is an example:
name = ""
if not name:
print("name is empty")
Output:
name is empty
Similarly, we can also use the len()
function in Python.
place = ""
# checking if string is empty
if len(place) == 0:
print('String is empty')
else:
print('String is not empty')
Output:
String is empty