- Python: Check if string is empty or blank or contain spaces only
- Check if a string is empty using len()
- Frequently Asked:
- Check if a string is empty using not
- Check if a string is empty or contain blank spaces only
- Using strip():
- Using isspace()
- Using Regex to check if a string is empty or contain blank spaces only in python
- Related posts:
- How to Check if a String is Empty in Python
- How to Create an Empty String in Python
- Are Strings of Spaces Considered Empty in Python?
- Use not To Check if a String is Empty in Python
- Use len to Check if a String in Empty in Python
- Use strip to Check if a String is Blank in Python
- Conclusion
- Additional Resources
Python: Check if string is empty or blank or contain spaces only
In this article we will discuss different ways to check if a given string is either empty or contains only spaces.
Check if a string is empty using len()
len() function in python accepts a sequence as an argument and returns the number of elements in that sequence. So, if we pass a string as an argument to the len() function, then it returns the total number of characters in that string.
So, we can use this len() function to check if a string is empty or not, by checking if number of characters in the string are zero or not i.e.
msg = "" # check if string is empty if len(msg) == 0: print('String is empty') else: print('String is not empty')
Frequently Asked:
But if our variable contains None or blank spaces then this solution will not work.
Check if a string is empty using not
An empty string in python is equivalent to False in python. So, to check if a string is empty or not we can just apply “not” operator with it i.e.
msg = "" # check if string is empty or not if not msg: print('String is empty') else: print('String is not empty')
Unlike previous solution, this solution will work even if variable contains None i.e.
msg = None # check if string is empty or not if not msg: print('String is empty or None') else: print('String is not empty')
Using this technique we can check if a given string is empty or None. But if string is blank i.e. contains only white spaces then both these solutions will also not work. Let’s discuss different techniques to check if string is empty or contain spaces only,
Check if a string is empty or contain blank spaces only
Using strip():
We can use the strip() function of string to get a copy of string without leading and trailing whites paces. So, let’s use this to check if string is empty or contains only white spaces i.e.
msg = " " # Check if string is empty or contain spaces only if msg and msg.strip(): print('String is neither empty nor blank') else: print('String is either None or Empty or contain spaces only')
String is either None or Empty or contain spaces only
It stripped all the white spaces from front and end of the string and converted the blank string to an empty string. Then it check it checked if string is empty or not.
Using isspace()
isspace() function of string class returns True if string contains only white spaces. So we can use this to check if string is empty or contain white spaces only i.e.
msg = " " # Check if string is empty or contain spaces only if msg and not msg.isspace(): print('String is neither empty nor blank') else: print('String is either None or Empty or Blank')
String is either None or Empty or contain spaces only
Using Regex to check if a string is empty or contain blank spaces only in python
We can create a regex pattern that checks if the given string is either empty or contains only white spaces i.e.
import re msg = " " # Check if string is empty or contain spaces only if not msg or re.search("^\s*$", msg): print('String is either empty or Blank or contain only spaces') else: print('String is neither empty nor blank')
String is either None or Empty or contain spaces only
Here we checked if the given string started with zero or more white spaces and contains only whitespaces after that, till the end.
Another example to check if string is empty or contain only spaces, using regex,
import re msg = "" # Check if string is empty or contain spaces only if not msg or re.search("^\s*$", msg): print('String is either empty or Blank or contain only spaces') else: print('String is neither empty nor blank')
String is either None or Empty or contain spaces only
So, here we discussed four different techniques to check if a given string is empty or blank in python.
The complete example is as follows,
import re def main(): print('*** Check if a string is empty using len() in Python *** ') msg = "" # check if string is empty if len(msg) == 0: print('String is empty') else: print('String is not empty') print('*** Check if a string is empty using "not" operator in python *** ') msg = "" # check if string is empty or not if not msg: print('String is empty') else: print('String is not empty') msg = None # check if string is empty or not if not msg: print('String is empty or None') else: print('String is not empty') print('Check if a string is empty by comparing with "" ') msg = "" if msg == "": print('String is empty') else: print('String is not empty') print('*** Check if a string is empty or contain blank spaces only ***') print('***** Check if a string is empty or contain blank spaces only using strip() ****') msg = " " # Check if string is empty or contain spaces only if msg and msg.strip(): print('String is neither empty nor blank') else: print('String is either None or Empty or contain spaces only') print('***** Check if a string is empty or contain blank spaces only using isspace() ****') msg = " " # Check if string is empty or contain spaces only if msg and not msg.isspace(): print('String is neither empty nor blank') else: print('String is either None or Empty or Blank') print('***** Using Regex to check if a string is empty or contain blank spaces only in python ****') print('Example 2:') msg = " " # Check if string is empty or contain spaces only if not msg or re.search("^\s*$", msg): print('String is either empty or Blank or contain only spaces') else: print('String is neither empty nor blank') print('Example 2:') msg = "" # Check if string is empty or contain spaces only if not msg or re.search("^\s*$", msg): print('String is either empty or Blank or contain only spaces') else: print('String is neither empty nor blank') if __name__ == '__main__': main()
*** Check if a string is empty using len() in Python *** String is empty *** Check if a string is empty using "not" operator in python *** String is empty String is empty or None Check if a string is empty by comparing with "" String is empty *** Check if a string is empty or contain blank spaces only *** ***** Check if a string is empty or contain blank spaces only using strip() **** String is either None or Empty or contain spaces only ***** Check if a string is empty or contain blank spaces only using isspace() **** String is either None or Empty or Blank ***** Using Regex to check if a string is empty or contain blank spaces only in python **** Example 2: String is either empty or Blank or contain only spaces Example 2: String is either empty or Blank or contain only spaces
Related posts:
How to Check if a String is Empty in Python
In this tutorial, you’ll learn how to use Python to check if a string is empty or not. Being able to work with strings and see whether or not they are empty is an important skill to learn. Python strings are immutable, meaning that they cannot be changed once they’re created. Because of this, it can be useful to check if a string is empty or not.
By the end of this tutorial, you’ll have learned:
- What is considered an empty string in Python
- How to use the not keyword to check if a string is empty
- How to check if a string is empty based on its length
How to Create an Empty String in Python
In Python, you can create an empty string in a number of ways. The simplest, and clearest, way is simply to create a string with nothing in it. This can be done using either single, double, or triple quotes. Let’s see what this looks like:
# Creating an Empty String in Python empty1 = '' empty2 = "" empty3 = """"""
Similarly, we can use the string constructor function str() to create an empty string in Python. Let’s see what this looks like and print it out:
# Creating an Empty String in Python empty = str() print(empty) # Returns #
Are Strings of Spaces Considered Empty in Python?
In the examples above, the strings were truly empty. Now, take a string like this for example:
Would we consider this empty? Well, it depends. From a technical perspective, the string isn’t empty. From a practical perspective, the string is empty. To get semantics out of the way, let’s consider a string that’s made up entirely of spaces or whitespace to be “blank”.
Use not To Check if a String is Empty in Python
The simplest, and most Pythonic, way of checking if a string is empty in Python is to simply use an if-else block with the not keyword. Empty strings in Python are considered to be falsy, meaning that they evaluate to False .
Because of this, we can easily check if a string is empty by checking its boolean truth:
# Checking if a String is Empty in Python string = '' print(not string) # Returns: True
What this expression evaluates to is not False , which then evaluates to True .
A cleaner, and more practical way to write this, is using an if-else block. With this we can run an expression when a string is empty or not:
# Checking if a String is Empty with an if-else string = '' if not string: print("Empty string!") else: print("Not empty string!") # Returns: # Empty string!
In the next section, you’ll learn how to check if a string is empty using the len() function.
Use len to Check if a String in Empty in Python
Because empty strings in Python are, well, empty, you can also check the length of a string to see if it’s empty or not. This is done using the len() function, which returns the length of an iterable object passed into it.
Let’s see how we can use this function to check if a string is empty or not:
# Using len() To Check if a String is Empty string = '' if len(string) == 0: print("Empty string!") else: print("Not empty string!") # Returns # Empty string!
Keep in mind that this only checks if a string is truly empty. In the following section, you’ll learn how to check if a string is either empty or blank.
Use strip to Check if a String is Blank in Python
In this section, you’ll learn how to check if a string is both empty as well as blank, meaning that it only contains whitespace. In order to do this, we first need to apply the .strip() method to the string, which returns a string without any whitespace. We can then use either not keyword or the len() function to check if it’s empty.
Let’s see what this looks like. We’ll create a string that contains a few spaces and combine the .strip() method with the not keyword:
# Checking if a String is Blank in Python string = ' ' if not string.strip(): print("Blank string!") else: print("Not blank string!") # Returns # Blank string!
Note that because strings in Python are immutable, the original string will not be modified.
Conclusion
In this tutorial, you learned how to use Python to check if a string is empty or blank. You first learned how to create an empty string in Python, using either direct assignment or the string constructor. Then, you learned how to check if a string was empty using the not keyword and the len() function. Following this, you learned how to work with blank strings, meaning strings that only contain whitespace.
Additional Resources
To learn more about related topics, check out the tutorials below: