Languages
[Edit]
EN

Python - check if string is empty

0 points
Created by:
Khloe-Kaiser
578

In this article, we would like to show you how to check if string is empty in Python.

Quick solution:

text = ""
print(bool(text))  # False
text = "abc"
print(bool(text))  # True

Note:

In Python empty strings are considered as false.

 

Practical example

In this example, we create a simple if statement to check if the string is empty or not.

Case 1: empty string

text = ""

if not text:
    print("true")
else:
    print("false")

Output:

true

Case 2: not empty string

text = "abc"

if not text:
    print("true")
else:
    print("false")

Output:

false

Note:

In this example, we don't need print() function because the expression itself returns true or false. It can be replaced with some action.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - string (popular problems)

Python - check if string is empty
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join