How to Check If a String is Empty or Contains Whitespaces in Python Last Updated : 28 Nov, 2024 Comments Improve Suggest changes 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 a String is Empty or Contains Whitespaces 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 Python - Check if String contains any Number We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = "Hello123" since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = "HelloWorld" since it has no digits the output should 2 min read Python - Test if string contains element from list Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string.Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. Pythons = "Python is powerful an 3 min read Check if String is Empty or Not - Python We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:Using Comparison Operator(==)The si 2 min read Check If API Response is Empty in Python In Python programming, determining whether an API response is empty holds importance for effective data handling. This article delves into concise techniques for checking whether the API response is empty or not, enabling developers to efficiently get rid of several data problems and enabling proper 2 min read Like