Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Create and Check an Empty String in Python

  • 27 Mar, 2025
  • Com 0
How to create and check an empty string in Python

An empty string in Python means there are no characters in it. Even spaces can be counted as characters, so empty means empty. Not a single character of any kind.

Empty StringIf a string is empty, its length will be 0.

Creating an empty string

You can create an empty string by assigning double quotes (“”) or single quotes (”) with nothing in between.

Create an empty string in Python

double_empty_string = ""
print(double_empty_string) # Output: ""
print(type(double_empty_string)) # Output: <class 'str'>

single_empty_string = ''
print(single_empty_string) # Output: ''
print(type(single_empty_string)) # Output: <class 'str'>

If you run the above program on the Python terminal, it will display an empty string by printing nothing.

There is a third way to initialize an empty string by using the str() constructor and passing nothing.

empty_string = str()

print(empty_string) # Output: ''

print(type(empty_string)) # Output: <class 'str'>

Check if a string is empty

In the Boolean context, an empty string is considered False. To check whether a string is empty, you can use the implicit boolean check using the “if not” condition.

Checking if string is empty using boolean check

double_empty_string = ""

# Checking If a String Is Empty
if not double_empty_string:
    print("String is empty")
    # Output: String is empty

Let’s check for non-empty strings:

Checking if string is not empty using boolean check

main_str = "Krunal"

# Checking If a String Is Empty
if not main_str:
    print("String is empty")
    
else:
    print("String is not empty") 
    # Output: String is not empty

Our code works fine! 

The time complexity of a Boolean check is O(1) because it directly checks the string’s internal flag for emptiness.

Using explicit length check

You can find the string’s length using the len() function and compare it with 0 using the “==” operator.

Using explicit length check

single_empty_string = ''

# Checking If a String Is Empty

# Explicit Length Check
if len(single_empty_string) == 0:
    print("String is empty")
    # Output: String is empty

If length is not 0, it is not empty, and does not execute the condition. If it is 0, it is empty.

Its time complexity is O(1) too because strings store their length as metadata, so this is a constant-time operation.

Using direct comparison

We can compare our input string with “==” to check if it is empty.

Using direct comparison

empty_string = str()

# Checking If a String Is Empty

# Direct Comparison to Empty String
if empty_string == "":
    print("String is empty")
    # Output: String is empty

That’s it!

Post Views: 122
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Create and Check an Empty Tuple in Python
How to Create and Check an Empty List in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend