Python initialize empty string | Example code

To initialize an empty string in Python, Just create a variable and don’t assign any character or even no space. This means assign “” to a variable to initialize an empty string.

empty_string = ""

Python initialize empty string Example

Simple example code. Initialize and check whether has a string that is empty or not using “Not operator“.

The strings are evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True.

str1= ""

if not str1:
    print("String is empty")

Output:

Python initialize empty string

Do comment if you have any doubts and suggestions on this Python string topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.