Python strings are immutable, i.e., we can change the strings according to the operations performed. String Manipulation is the most important feature in python. You can manipulate the string by many methods such as String slicing techniques, looping over the elements, and string methods. But. Sometimes we come to the situation where we need to check if the Python string is empty or not and the list is empty. In this article, we will be discussing ways to check if the string is empty or not in Python.
NOTE : String which has spaces in them are empty string but has a non-zero size.
7 Different ways to check if the string is empty or not
Let us discuss certain ways through which we can check if the string is an empty string or not.
1. Using len() method
We will be calculating the length of the string with the help of len() in python. Then, we will check if the string’s length is equal to 0, then the string is empty; otherwise, not.
NOTE: If the string contains spaces in it the string is not treated as the empty string by using the len() function in python
Let us look at the example for the better understanding of the concept:
#input empty with and without spaces string
s = ""
str = " "
x = len(s)
y = len(str)
if x == 0:
print("string is empty")
else:
print("string is not empty")
if y == 0:
print("string is empty")
else:
print("string is not empty")
Output:
string is empty
string is not empty
Explanation:
Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. Then, we have calculated the length of both the strings with the len() function in python. Then, we have applied the if-else loop and checked if the string’s length is equal to 0, then the print string is empty, and if it is not empty, print not empty. In the len(), whitespaces are not treated as empty in the string, giving the output as a not empty string. At last, we have printed the output of both the string.
2. Using not operator To Check If String Is Empty String In Python
Not operator is used to perform the same task as of len() function. The empty string is always equivalent to False in Python. If we want to check if the string is empty or not, we can check it with the help of not operator.
NOTE: If the string contains spaces in it the string is not treated as the empty string by using the not operator in python
Let us look at the example for the better understanding of the concept:
#input empty with and without spaces string
s = ""
str = " "
if not s:
print("string is empty")
else:
print("string is not empty")
if not str:
print("string is empty")
else:
print("string is not empty")
Output:
string is empty
string is not empty
Explanation:
Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. Then, we have applied if else condition in which we have checked if not in string, then the string is empty. Otherwise, it is not empty. The not operator also does not treat whitespaces as the empty string so that the output will come as not an empty string of the second input string. At last, we have printed the output.
3. Using and operator + strip() function to check if string is empty string in python
Sometimes, we see that the whitespaces present in the string are not treated as empty strings. So, when we check for the empty string, we can also check for the whitespaces through the strip() function.
Let us look at the example for the better understanding of the concept:
#input empty with and without spaces string
s = ""
str = " "
if s and s.strip():
print("string is not empty")
else:
print("string is empty")
if str and str.strip():
print("string is not empty")
else:
print("string is empty")
Output:
string is empty
string is empty
Explanation:
Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have applied the if-else condition and checked with the strip() function that the string is empty or not. If the condition gets False, that means the string is empty, and else blocks get executed. In this function, whitespace is also treated as empty strings. At last, we have printed the output.
4. Using Strip() function only To Check Empty String In Python
Sometimes, we see that the whitespaces present in the string are not treated as empty strings. So, when we check for the empty string, we can also check for the whitespaces through the strip() function.
Let us look at the example for the better understanding of the concept:
#input empty with and without spaces string
s = ""
str = " "
if s.strip():
print("string is not empty")
else:
print("string is empty")
if str.strip():
print("string is not empty")
else:
print("string is empty")
Output:
string is empty
string is empty
Explanation:
Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have applied if else condition in which we strip the strings, i.e., whitespaces gets removed, and then if the string gets empty, it will be an empty string. Otherwise, the string is not empty. At last, we have printed the output.
5. Using not operator + str.isspace()
It works the same as the strip() function as it checks for the whitespaces in the strip. But, strip() function a lot of time as compared to str. isspace() function because strip() has to perform strip operation, which takes a lot of computation loads.
Let us look at the example for the better understanding of the concept:
#input empty with and without spaces string
s = ""
str = " "
if s and not s.isspace():
print("string is not empty")
else:
print("string is empty")
if str and not str.isspace():
print("string is not empty")
else:
print("string is empty")
Output:
string is empty
string is empty
Explanation:
Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have applied the if-else condition. In the if-else condition, we have applied the isspace() function, which will check all the string spaces. Finally, we have printed the output, and you can see the output that both the strings are empty.
6. Using __eq__ method
The dunder basically tells the methods that have double underscores before and after their names. We can check empty string through the __eq__ method also.
NOTE: If the string contains spaces in it the string is not treated as the empty string by using the __eq__ method in python
Let us look at the example for the better understanding of the concept:
#input empty with and without spaces string
s = ""
str = " "
if "".__eq__(s):
print("string is empty")
else:
print("string is not empty")
if "".__eq__(str):
print("string is empty")
else:
print("string is not empty")
Output:
string is empty
string is not empty
Explanation:
Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have taken the __eq__ method. We have applied the given method in the if-else condition and checked if the string is empty or not. At last, we have printed the output. This method also doesn’t treat whitespaces as empty strings.
7. Using Regex To Check Empty String In Python
We can create a regex pattern to check if the given string is either empty or contains only white spaces.
Let us look at the example for the better understanding of the concept:
#import re library
import re
s = ""
str = " "
if not s or re.search("^\s*$", s):
print('String is empty or has spaces')
else:
print('String is not empty')
if not s or re.search("^\s*$", s):
print('String is empty or has spaces')
else:
print('String is not empty')
Output:
String is empty or has spaces
String is empty or has spaces
Explanation:
Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have imported the re library in python. Then, we have applied the if-else condition with the given format to check whether the string is empty or not. If the if-condition satisfies, then the output gets printed as it is an empty string. Otherwise, Output gets printed as not an empty string. Hence, you can see the output.
Also Read: 5 Ways to Check if a String is Integer in Python
Add Empty Strings In Python
To add a string to an empty string in Python, you can simply use the + operator. Here’s an example:
empty_string = ""
new_string = "Hello, World!"
result = empty_string + new_string
print(result) # Output: "Hello, World!"
In this example, we create an empty string variable called empty_string. We then create a new string variable called new_string, which contains the text “Hello, World!”.
To add new_string to empty_string, we use the + operator. When we print the result variable, we get the output “Hello, World!”.
Empty String In Python As Boolean
In Python, the empty string “” is considered false in boolean contexts, meaning it will evaluate to False.
Here is an example:
my_string = ""
if my_string:
print("The string is not empty.")
else:
print("String is empty.")
In this example, we create a variable called my_string and assign it an empty string. We then use an if statement to check if my_string is true or false. Since my_string is empty, it evaluates to False and the code executes the else block and prints “String is empty.”
However, if we assign a non-empty string to my_string like this:
my_string = "Hello world!"
and you run the same code, the if statement will evaluate to True and the code will execute the if block and print “String is not empty.”
Empty String Join Python
You can use the join() method to concatenate a list of strings into a single string with an empty string as the separator.
Consider the given example for better interpretation:
my_list = ["apple", "banana", "orange"]
result = "".join(my_list)
print(result)
# Output: "applebananaorange"
In this example, we create a list called my_list that contains three strings. We then use the join() method to concatenate the strings in the list into a single string. The empty string “” is used as the separator, which means that no separator will be inserted between the individual strings.
When we print the result variable, we get the output “applebananaorange”.
Note that if the list contains only one string or no strings at all, the join() method will return the original string or an empty string respectively.
FAQs
“” can be used to declare an empty string in Python. It doesn’t have any character, hence, it’s empty.
You can represent None variable as a representation of None data type in Python. It doesn’t mean that an empty string will imply None. For a string to be None, it should have None as a data type which is not an empty string.
A null string can be interpreted as empty string in Python. Its length is zero.
Conclusion
In this tutorial, we have learned about the concept of checking the string is empty or not and if the string contains whitespaces only or not. We have explained the full concept in detail by taking all the ways to check if the string is empty. All the ways are explained in detail with the help of examples. You can use any of the methods you like to use according to your program or project requirement.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.