How to Check If String is Empty or Not in Python

How to Check If String is Empty or Not in Python

To check an empty string in Python, use not operator, or use len() function, or use not + string.strip() or use not + string.isspace() ways.

String data type in Python is immutable; that means you can not change a string once defined. String with spaces is actually an empty string but has non-zero size. So, when you check the empty string with len() or not operator, it counts space as a character in the string, and hence it won’t count a string with space as an empty string.

How to check an empty string in Python

To check an empty string in Python, you can use one of the following methods.

  1. Using not operator
  2. Using len() function
  3. Using not + string.strip()
  4. Using not + string.isspace()

Not operator to check empty string

The not operator in Python checks the string with just spaces to be non-empty, which should not practically be True.

## app.py

strA = ""

## checking if string is empty
print("Check if the strA is empty : ", end="")
if(not strA):
    print("The string is empty")
else:
    print("No it is not empty")
Output
Check if the strA is empty : The string is empty

The string is empty, and one thing to note that we did not even put the space in the string. That is why it returns as empty. If we put a space in the string, it will not count as empty, and the not operator returns False.

## app.py

strA = " "

## checking if string with space is empty
print("Check if the strA with space is empty : ", end="")
if(not strA):
    print("The string is empty")
else:
    print("No it is not empty")

python programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Guide to Python Programming Language

Guide to Python Programming Language

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.