How to Check If a String is Empty or Contains Whitespaces in Python Last Updated : 28 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Sometimes, we must check if a string is empty or only contains Whitespaces. This check is useful when we are processing user input or handling data. Another simple way to check if a string is empty is by using the len() function. This checks the length of the string. Python s = "" if len(s) == 0: print("empty") else: print("not empty") Outputempty Let's look into various other methods to Check if string is empty or contains whitespace. Table of ContentUsing isspace() to Check for WhitespacesUsing len() and s.strip()Using isspace() to Check for WhitespacesIf we only want to check if a string contains spaces (and nothing else), we can use the isspace() method. This method returns True if the string contains only whitespace characters. Python s = " " if s.isspace(): print("whitespaces") else: print("not whitespaces") Outputwhitespaces Using len() and s.strip()strip() method helps to remove all spaces so if the result is an empty string, we know the original string was just whitespace. If the string is already empty then len(s) will help toreturn zero. Python s = " " if not s.strip(): print("whitespaces") elif len(s) == 0: print("empty") else: print("contains text") Outputwhitespaces Comment More infoAdvertise with us Next Article How To Check If Variable Is Empty In Python? P pragya22r4 Follow Improve Article Tags : Python Python Programs python-string Python string-programs Practice Tags : python Similar Reads How to Check if Tuple is empty in Python ? A Tuple is an immutable sequence, often used for grouping data. You need to check if a tuple is empty before performing operations. Checking if a tuple is empty is straightforward and can be done in multiple ways. Using the built-in len() will return the number of elements in a tuple and if the tupl 2 min read How To Check If Variable Is Empty In Python? Handling empty variables is a common task in programming, and Python provides several approaches to determine if a variable is empty. Whether you are working with strings, lists, or any other data type, understanding these methods can help you write more robust and readable code. In this article, we 2 min read Python | Ways to check if given string contains only letter Given a string, write a Python program to find whether a string contains only letters and no other keywords. Let's discuss a few methods to complete the task. Method #1: Using isalpha() method Python3 # Python code to demonstrate # to find whether string contains # only letters # Initialising string 3 min read How to Check if a Variable is a String - Python The goal is to check if a variable is a string in Python to ensure it can be handled as text. Since Python variables can store different types of data such as numbers, lists or text, itâs important to confirm the type before performing operations meant only for strings. For example, if a variable co 3 min read Python | Check if any String is empty in list Sometimes, while working with Python, we can have a problem in which we need to check for perfection of data in list. One of parameter can be that each element in list is non-empty. Let's discuss if a list is perfect on this factor using certain methods. Method #1 : Using any() + len() The combinati 6 min read Check Whether String Contains Only Numbers or Not - Python We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains 3 min read Like