To check if a string is empty in Python, you can use the len()
function to get the length of the string.
In this example,
str1
with an empty string value ""
.len()
function on str1
to get the length of the string.0
, the string is empty.str1 = ""
isEmpty = len(str1) == 0
print(f"Is string empty: {isEmpty}")
Is string empty: True
In this example,
str2
with a non-empty string value "Hello World"
.len()
function on str2
to get the length of the string.0
, the string is empty.str2 = "Hello World"
isEmpty = len(str2) == 0
print(f"Is string empty: {isEmpty}")
Is string empty: False
In this tutorial, we learned How to Check if String is Empty in Python language with well detailed examples.