OneBite.Dev - Coding blog in a bite size

check if a string is empty in python

Code snippet on how to check if a string is empty in python

  if not len(string):
      print("String is empty")
  else:
      print("String is not empty")

This code checks is a string input is empty or not. The if statement first checks if the length of the string is equal to 0 or not (which is the same as checking if the string is empty or not). If the length of the string is 0, it executes a print statement that says “String is empty”. If, however, the length of the string is not 0, it executes a print statement that says “String is not empty”.

python