EN
Python - check if string is empty
0 points
In this article, we would like to show you how to check if string is empty in Python.
Quick solution:
xxxxxxxxxx
1
text = ""
2
print(bool(text)) # False
xxxxxxxxxx
1
text = "abc"
2
print(bool(text)) # True
Note:
In Python empty strings are considered as
false
.
In this example, we create a simple if
statement to check if the string is empty or not.
Case 1: empty string
xxxxxxxxxx
1
text = ""
2
3
if not text:
4
print("true")
5
else:
6
print("false")
Output:
xxxxxxxxxx
1
true
Case 2: not empty string
xxxxxxxxxx
1
text = "abc"
2
3
if not text:
4
print("true")
5
else:
6
print("false")
Output:
xxxxxxxxxx
1
false
Note:
In this example, we don't need
print()
function because the expression itself returnstrue
orfalse
. It can be replaced with some action.