Sometimes, we want to check if a string is empty with Python.
In this article, we’ll look at how to check if a string is empty with Python.
How to check if a string is empty with Python?
To check if a string is empty with Python, we can use the not
operator.
For instance, we write:
some_string = ''
if not some_string:
print('string is empty')
We put not
before some_string
to check if it’s empty.
We can do this since an empty string is falsy.
Therefore, 'string is empty'
should be printed.
Conclusion
To check if a string is empty with Python, we can use the not
operator.