| by Arround The Web | No comments

How Do I Check If a String Is Empty in Python

Checking if a string is empty or not is an important task for any Python developer, as it can be used for various purposes such as validating user input and other operations. Python provides different methods such as the “len()” function, “not” operator, and “strip()”, etc. to check if a string is empty or not. These approaches can also be utilized to deal with whitespace and empty strings.

In this article, we will present the following methods for determining whether a string is empty:

Method 1: Using the “not” Operator

The “not” operator is used to return “true” if the condition is not fulfilled. This operator can be applied to determine if an initialized string is empty.

Example

Let’s overview the following code:

string_a = ""
if (not string_a):
    print("String is Empty")
else:
    print("String is not Empty")

 

In the above example snippet, the “if/else” statement is used along with the “not” operator to check if the input string is empty or not by returning the corresponding message.

Output

The outcome implies that the input string is empty.

Method 2: Using the “len()” Function

The inbuilt “len()” function is used to get the length of the given iterable such as string, list, etc. This function is used combined with the “==” operator to determine if the input string is empty or not.

Syntax

len(object)

 

In the above syntax, “object” is the object of which we have to find the length, such as a “string”, “list”, “tuple”, etc.

Example

Let’s go through the given below code:

string_value = ""
if len(string_value) == 0:
    print("String is Empty")
else:
    print("String is not Empty")

 

In this example, the “if/else” statement is used along with the “len()” function to check whether the specified string is empty or not by referring to the length of the string.

Output

The outcome signifies that the initialized string is empty.

Method 3: Using the “strip()” Function

The “strip()” function is used to retrieve a copy of the initialized string with leading and ending characters omitted. In this example, this function can be implemented to determine whether there is an empty string.

Syntax

string.strip([chars])

 

Here, “chars” is an optional parameter that specifies the characters that will be removed and if “chars” are not given, it removes the whitespaces by default.

Example

Let’s take a look at the following code:

string_value = "   "
if string_value.strip() == "":
    print("String is Empty")
else:
    print("String is not Empty")

 

In the above code, the “string_value.strip()” function is used along with the “if/else” statement to check whether the given string is empty or not by removing the beginning and ending characters.

Output

The string is empty in the above output.

Method 4: Using “==” Operator

The Python “==” operator is used to compare two objects. This operator can also be utilized to verify whether the empty string is empty or not via equality check.

Example

Here is an example code:

string_value = ""
if string_value == "":
    print("String is Empty")
else:
    print("String is not Empty")

 

According to the above lines of code, the “if” statement is used with the “==” operator to check whether the given string is empty or not by equalizing it with “”.

Output

In the above outcome, it can be seen that the initialized string is empty.

Method 5: Using “__eq__()” Method

The “__eq()__” method in Python is a special method that is utilized to evaluate two objects by their values. This method can also be utilized to determine whether a string is empty.

Example

Let’s understand it by the following code snippet:

string_value = ""
if string_value.__eq__(""):
    print("String is Empty")
else:
    print("String is not Empty")

 

In the above example, the “string_value.__eq__()” function takes the empty string as an argument and checks whether the passed string is empty or not via the “if/else” statement.

Output

The above snippet implies that the initialized string is empty.

Method 6: Using “not + str.isspace()”

The “str.isspace()” function is used to check if a string contains only whitespace characters, such as spaces, tabs, newlines, etc. This function can also be applied along with the “not” operator to check whether the input string is empty or not.

Syntax

str.isspace()

 

A string whose entire content consists of whitespace characters will be returned as “true”, otherwise it will be returned as “false”.

Example

Let’s understand it via the following code example:

string1 = " "
if(string1 and not string1.isspace()):
    print("String is Not Empty")
else:
    print("String is Empty")

 

In the above code block:

  • The “if/else” statement is used to test two conditions by using the logical operators “and” with “not”, respectively.
  • The “ifstatement condition is “true” only if both the conditions are satisfied, that is if the string is not empty and not whitespace.
  • Otherwise, the “else” statement will come into effect.

Output

Based on the above results, there is no data in the initialized string.

Conclusion

The “not” operator, “len()” function, “strip()” function, “==” operator, “__eq__()” method, or “not + str.isspace()” method can be used to determine whether or not a string is empty. The “not” operator can be utilized individually or along with the “str.isspace()” method to determine if the specified string is empty or not. The “strip()” method and other approaches can also efficiently check whether the given string contains the value or not. This post illustrated various approaches to check if the initialized string is empty or not using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply