Does Python have something like an empty string variable where you can do:
if myString == string.empty:
Regardless, what\'s the most elegan
I find hardcoding(sic) "" every time for checking an empty string not as good.
Doing this: foo == ""
is very bad practice. ""
is a magical value. You should never check against magical values (more commonly known as magical numbers)
What you should do is compare to a descriptive variable name.
One may think that "empty_string" is a descriptive variable name. It isn't.
Before you go and do empty_string = ""
and think you have a great variable name to compare to. This is not what "descriptive variable name" means.
A good descriptive variable name is based on its context. You have to think about what the empty string is.
You are building a form where a user can enter values. You want to check if the user wrote something or not.
A good variable name may be not_filled_in
This makes the code very readable
if formfields.name == not_filled_in:
raise ValueError("We need your name")
You are parsing CSV files and want the empty string to be parsed as None
(Since CSV is entirely text based, it cannot represent None
without using predefined keywords)
A good variable name may be CSV_NONE
This makes the code easy to change and adapt if you have a new CSV file that represents None
with another string than ""
if csvfield == CSV_NONE:
csvfield = None
There are no questions about if this piece of code is correct. It is pretty clear that it does what it should do.
Compare this to
if csvfield == EMPTY_STRING:
csvfield = None
The first question here is, Why does the empty string deserve special treatment?
This would tell future coders that an empty string should always be considered as None
.
This is because it mixes business logic (What CSV value should be None
) with code implementation (What are we actually comparing to)
There needs to be a separation of concern between the two.
Test empty or blank string (shorter way):
if myString.strip():
print("it's not an empty or blank string")
else:
print("it's an empty or blank string")
If you just use
not var1
it is not possible to difference a variable which is boolean False
from an empty string ''
:
var1 = ''
not var1
> True
var1 = False
not var1
> True
However, if you add a simple condition to your script, the difference is made:
var1 = False
not var1 and var1 != ''
> True
var1 = ''
not var1 and var1 != ''
> False
The most elegant way would probably be to simply check if its true or falsy, e.g.:
if not my_string:
However, you may want to strip white space because:
>>> bool("")
False
>>> bool(" ")
True
>>> bool(" ".strip())
False
You should probably be a bit more explicit in this however, unless you know for sure that this string has passed some kind of validation and is a string that can be tested this way.
Below is a simple method to identify an empty string:
a="" #Empty string
if(len(a)==0): #len() is used to find the length of the string or variable inside its' brackets.
print("Empty")
else:
print("Filled")
The above len() method is an inbuilt function that helps us to find the length of a certain string or variable. It can also be used in the case of lists.
LOGIC: If the length of the character or variable is 0 then absolutely it means that the string or variable is empty. BUT remember that this will work only in the case of strings and lists and not with integers. Below is the code to find length of integers.
a=int(input())
if(len(str(a))==0):
print(True)
In the above code str() is used to convert an int to a string format like from 123 to its string format "123". In the string format it will be valid.
if stringname:
gives a false
when the string is empty. I guess it can't be simpler than this.