to check whether x is True or False:

Copybool("")
> False

bool("x")
> True

for details on the semantics of is and == see this question

Answer from scytale on Stack Overflow
🌐
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.
Discussions

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
Why does my empty dictionary not evaluate to false?
Because an empty dictionary is not equal to false. In a boolean context, it's falsey (treated as false), but that's different from it literally comparing as the same as false. I keep thinking if a list or dictionary is empty it automatically evaluates to none. No, it's just equal to an empty list/dictionary. It will only evaluate to None if you use it in an expression like: {} or None # None More on reddit.com
🌐 r/learnpython
8
2
December 21, 2022
.Empty string comprehension?
Hello everyone , I wrote this code that I understand except one detail in it , here’s the code : person_infos(first_name, last_name, age=''): person = {'first': first_name, 'last': last_name} if age: per… More on discuss.python.org
🌐 discuss.python.org
1
March 14, 2020
🌐
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 - In this article, you have learned different ways to check if the string is empty or not in Python. Since empty strings are considered as false in Python, we can directly use it on the if condition to check or use it with either not operator ...
🌐
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 - The len() function returns the length of a string so if the length is 0, it means that string is empty: ... The string is empty. ... If len(s) == 0 then the string is empty. In Python, empty strings are treated as "Falsy".
🌐
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 - The empty string is the only possible zero-length string. All other strings return True. An if statement in Python must have a Boolean argument. So you don't have to write "if bool(myString)". "if myString" is syntactic sugar for calling bool(myString). Also, you don't have to explicitly write ...
Find elsewhere
🌐
Better Stack
betterstack.com › community › questions › how-to-check-if-string-is-empty-in-python
How to check if the string is empty in Python? | Better Stack Community
February 3, 2023 - For example: ... Alternatively, you can use the bool() function to check if the string is empty. An empty string is considered to be False, and all other strings are considered to be True:
🌐
Esri Community
community.esri.com › t5 › python-questions › quot-if-not-null-and-not-empty-string-quot-paradox › td-p › 1618009
Solved: "If not Null and not empty string" paradox - help ... - Esri Community
May 29, 2025 - From 6. Expressions - Python 3.y.z documentation: In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, ...
🌐
Syntax Scenarios
syntaxscenarios.com › python › check-if-a-string-is-empty
How to Check if a String is Empty in Python? – Syntax Scenarios
December 13, 2024 - Python’s bool() function evaluates the truth value of an object. In the case of an empty string, bool() will return False, and for non-empty strings, it will return True.
🌐
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)
🌐
Enterprise DNA
blog.enterprisedna.co › python-empty-string
Python Empty String: Explained With Examples – Master Data Skills + AI
The is_empty variable will now be a list of boolean values. If a value returns True, it’s an empty string. If it returns False, it is a non-empty string. When you print the is_empty variable, you will see this output: ... In other words, three values from five have a non-zero size and two ...
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1204 › handouts › py-string.html
Python Strings
In this and other string comparisons, characters much match exactly, so 'a' matches 'a', but does not match 'A'.(Mnemonic: this is the same word "in" as used in the for-loop.) >>> 'c' in 'abcd' True >>> 'c' in 'ABCD' False >>> 'aa' in 'iiaaii' # test string can be any length True >>> 'aaa' in 'iiaaii' False >>> '' in 'abcd' # empty string in always True True
🌐
Quora
quora.com › What-is-the-difference-between-Null-and-empty-string-in-Python
What is the difference between Null and empty string in Python? - Quora
Answer (1 of 21): Null is a special reserved pseudo variable that represents nothing - it’s strictly locked to denote nothing and cannot be aloted a pointer in terms of Memory reference - because it has nothing and is nothing. Empty strings are like Containers.
🌐
Python.org
discuss.python.org › python help
.Empty string comprehension? - Python Help - Discussions on Python.org
March 14, 2020 - Hello everyone , I wrote this code ... return person a = person_infos('fna', 'lna', age=34) print(a) The variable age in the function definition is an empty string , but we when set age as a number ( age = 4 ) no error happens , ...
🌐
Astral
docs.astral.sh › ruff › rules › compare-to-empty-string
compare-to-empty-string (PLC1901) | Ruff
The --preview flag is required for use. Checks for comparisons to empty strings. An empty string is falsy, so it is unnecessary to compare it to "". If the value can be something else Python considers falsy, such as None, 0, or another empty container, then the code is not equivalent.
🌐
PythonHow
pythonhow.com › how › check-if-a-string-is-empty
Here is how to check if a string is empty in Python
The 'not' operator returns True if the string is empty and False if it is not. ... In Python, you can check if a string is empty by using the 'not' operator as shown in the code above.
🌐
Medium
medium.com › @theconnoisseur › why-false-has-a-truth-value-of-true-in-python-14b09bb6e619
Why “False” Has a Truth Value of True in Python | by TheConnoisseur | Medium
July 26, 2023 - When used in conditional statements or boolean operations, they are treated as representing false conditions. Here are some examples of falsy values in Python: Empty strings: An empty string "" is considered falsy.