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 do not need to differentiate between an empty string and some other falsy value. To explicitly check for an empty string use the following:
if myString == "":
See the documentation on Truth Value Testing for other values that are false in Boolean contexts.
Answer from Andrew Clark on Stack OverflowEmpty 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 do not need to differentiate between an empty string and some other falsy value. To explicitly check for an empty string use the following:
if myString == "":
See the documentation on Truth Value Testing for other values that are false in Boolean contexts.
From PEP 8, in the “Programming Recommendations” section:
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
So you should use:
if not some_string:
or:
if some_string:
Just to clarify, sequences are evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True.
What’s the difference between None and an Empty String (“ “)?
.Empty string comprehension?
Is there a better way to make an empty string?
Meaning of empty string "" in this example
Videos
I recently came across a problem in a textbook where these 2 were interchangeable. So what’s the difference between these 2 and when to use which? Thanks!
Edit for context:
would like to clarify the use of None in this question to initialize the while loop, how does it initialize the loop?
start = None
while start != “”: start = int(input(“Start: “) . . .
» pip install python-dotenv