Initialize empty string in Python
An empty string in Python is simply a string that has no characters in it. It is often used when we need to represent "nothing" or initialize a string variable.
In this article, we will explore how to create an empty string in Python using different methods.
Using Two Quotation Marks
The simplest way to define an empty string is by using two quotation marks with nothing in between. These can be single (') or double (") quotes. Both are valid.
s = ""
print(s)
Using str() constructor
You can also use Python’s built-in str() function to create an empty string. This is useful when you want to ensure that the variable is always a string.
s = str()
print(s)
Assigning an Empty String via Variable
Sometimes, we may want to initialize a string variable and leave it empty for now. This is common when we plan to build the string step by step later in our program.
s = ""
# We can later add more characters to it
s += "Hello"
print(s)
Output
Hello