To create an empty string in Python, you can use single or double quotes.
empty_string_with_single_quotes = ''
empty_string_with_double_quotes = ""
When working with string variables and text in Python, the ability to create variables with specific values can be useful.
One such string which is unique is the empty string. You can create an empty string in two ways.
To create an empty string in Python, you can use single or double quotes.
Below shows how you can create empty strings in Python.
empty_string_with_single_quotes = ''
empty_string_with_double_quotes = ""
What’s the Purpose of an Empty String in Python?
Empty strings can be useful if you want to build a new string from scratch. You can think using an empty string just like if you wanted to initialize a count variable at 0.
You can also use empty strings when you want to check and validate input from the user. If a variable is equal to an empty string, then it is possible the user forgot to fill out a field and we should alert the user or client about this situation.
How to Check if a Variable is Equal to Empty String
You can easily check if a string variable is empty in your Python code.
Below is an example showing how you can check if a variable is equal to an empty string in Python.
empty_string = ""
other_string = "word"
print(empty_string == "")
print(other_string == "")
#Output:
True
False
Properties of Empty Strings in Python
There are a few properties of empty strings in Python which you should know.
First, the length of a string that is empty is 0.
empty_string = ""
print(len(empty_string))
#Output:
0
Second, when you convert an empty string to a boolean value, the new boolean value is False.
empty_string = ""
print(bool(empty_string))
#Output:
False
Another property is that the empty string is contained in all string variables in Python.
print("" in "string example")
#Output:
True
Hopefuly this article has been useful for you to learn how to create empty string variables in python