Empty strings are "falsy" (python 2 or python 3 reference), which means they are considered false in a Boolean context, so you can just do this:

if not myString:

This is the preferred way if you know that your variable is a string. If your variable could also be some other type then you should use:

if myString == "":

See the documentation on Truth Value Testing for other values that are false in Boolean contexts.

Answer from Andrew Clark on Stack Overflow
🌐
Wyzant
wyzant.com › resources › ask an expert
Most elegant way to check if the string is empty in Python? | Wyzant Ask An Expert
May 22, 2019 - I find hard coding `""` every time for checking an empty string not as good. ... John L. answered • 05/24/19 ... All Python types have the ability to be evaluated for "truthiness," which is returned by the built-in Boolean conversion function, bool().
🌐
TutorialsPoint
tutorialspoint.com › what-is-the-most-elegant-way-to-check-if-the-string-is-empty-in-python
What is the most elegant way to check if the string is empty in Python?
April 21, 2025 - The string is empty. The Python len() function is used to determine the length of an object (such as list, string or dictionary).
Discussions

How to check if the string is empty in Python? - Stack Overflow
Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, what's the most elegant way to check for empty string values? I find hard coding... More on stackoverflow.com
🌐 stackoverflow.com
What's the most pythonic way of checking if variable is None or empty string ""?
Both None and "" are falsy values so simply if not variable: will cover both those cases. This doesn't explicitly check for those values but if you're just checking for something not being set this is probably the way to go. More on reddit.com
🌐 r/learnpython
9
4
July 6, 2021
empty string returns True upon checking if its contained in a non empty string
Yes, empty strings are in any other string. Like the empty set is a subset of any other set. I feel like there’s probably many easier ways to do what you’re trying to do here (which seems to be looking for the string “di” within another string without case sensitivity?) but to minimize the change to your approach, if you want to initialize previous so it returns false without raising a type error, you could initialize it with a long string that can’t possibly be in the search string. After the first iteration it will be set by the loop code and won’t be empty again. More on reddit.com
🌐 r/learnpython
11
6
June 7, 2025
In Java, should I use (String ) isEmpty() or isBlank() when checking an XML object for an empty value?
What behavior do you want if the xml parser emits " " as the config value? More on reddit.com
🌐 r/AskProgramming
4
1
September 26, 2022
🌐
openHAB Community
community.openhab.org › setup, configuration and use › scripts & rules
Python: String empty or null? - Scripts & Rules - openHAB Community
February 25, 2021 - Hey guys, one maybe very easy question: I try to check if a string item is empty, therefore I try it with: if len(ir.getItem(presenceLightAlter).state.StringValue()) > 0: But I get an error, because the item state is…
🌐
30 Seconds of Code
30secondsofcode.org › home › python › string is empty
How can I check if a string is empty in Python? - 30 seconds of code
August 5, 2022 - There's a straightforward answer to this that takes advantage of the truth value of strings. In Python, any object can be tested for truth value, including strings. In this context, strings are considered truthy if they are non-empty, meaning they contain at least one character.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-check-if-string-is-empty-or-not
Check if String is Empty or Not - Python - GeeksforGeeks
July 11, 2025 - If len(s) == 0 then the string is empty. In Python, empty strings are treated as "Falsy".
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › check if string is empty or not in python
Check if String is Empty or Not in Python - Spark By {Examples}
May 21, 2024 - The __eq__() method is a special method, it is one of the so-called “dunder” methods (short for “double underscore”), which are special methods in Python that have a particular syntax, beginning and ending with two underscores. # Using __eq__() first="" if ""__.eq__(first): print("Empty string") else: print("Not empty string") # Output: # Empty string
🌐
freeCodeCamp
freecodecamp.org › news › how-to-check-if-a-list-is-empty-in-python
Python isEmpty() equivalent – How to Check if a List is Empty in Python
April 19, 2023 - In this article, we saw how to check if a list is empty in Python by using three different methods.
🌐
Sentry
sentry.io › sentry answers › python › check if a string is empty in python
Check if a string is empty in Python | Sentry
May 15, 2023 - if my_string == "": print("my_string is an empty string!") else: print("my_string is not an empty string!")
🌐
Sololearn
sololearn.com › en › Discuss › 1779570 › is-an-empty-string-true-or-false-in-python-3
Is an empty string true or false in python 3 | Sololearn: Learn to code for FREE!
April 29, 2019 - Saksham Jain Both ''==False and '' ==True are False because an empty string is not a boolean expression. It has a boolean value which can be found out with bool(''). But an empty string is not equivalent to False.
🌐
Python
docs.python.org › 3 › library › os.path.html
os.path — Common pathname manipulations
This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
4 days ago - This is a relative of setattr(). The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example, delattr(x, 'foobar') is equivalent to del x.foobar.
🌐
Stack Abuse
stackabuse.com › how-to-check-if-a-string-is-empty-or-none-in-python
How to Check if a String is Empty or None in Python
June 5, 2023 - In both of these methods, the if statement will print "String is empty" if the string s is empty. The not operator in the second example negates the "truthiness" or "falsiness" of the string, making the code slightly more concise. This is a common Pythonic way to check for empty strings.
🌐
Reddit
reddit.com › r/learnpython › empty string returns true upon checking if its contained in a non empty string
r/learnpython on Reddit: empty string returns True upon checking if its contained in a non empty string
June 7, 2025 -

This code is meant to count all the words that have the expression "di" but with the text "imagina." the final answer is 1.

texto = 'imagina.'
cl = 0
flag_di = False
answer = 0
previous = ''

for car in texto:
    if car != ' ' and car != '.':
        cl += 1
        
        
        if car in 'iI' and previous in 'dD':
            flag_di = True
            
        previous = car
    
    else:
        if car == ' ' or car == '.':
            if flag_di:
                answer += 1
                
            cl = 0
            flag_di = False
            previous = ''
    
print(answer)
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-check-if-a-string-is-empty-or-contains-whitespaces-in-python
How to Check If a String is Empty or Contains Whitespaces in Python - GeeksforGeeks
July 23, 2025 - Sometimes, we must check if a string is empty or only contains Whitespaces. This check is useful when we are processing user input or handling data. Another simple way to check if a string is empty is by using the len() function.