Python: Check if String is Empty or Contain Spaces only

Python strings are byte arrays representing characters of Unicode.. However, because Python lacks a character data type, a single character is simply a one-length string. Square brackets can be used to access string elements.

Given a string, the task is to check whether the string is empty or not.

Example:

Input:

string=""

Output:

Given String is empty

Check if String is Empty

There are several ways to check whether the string is empty some of them are:

Method #1:Using len() function

The most general method for checking for zero-length strings is to use len(). Despite the fact that it ignores the fact that a string with only spaces should be considered an empty string even if it is not zero.

In Python, the len() function takes a sequence as an argument and returns the number of elements in that sequence. So, if we pass a string to the len() function as an argument, it returns the total number of characters in that string.

So, we can use the len() function to determine whether a string is empty or not by determining whether the number of characters in the string is zero or not.

Below is the implementation:

# given string
string = ""
# determining whether or not the given string is empty using len()
if len(string) == 0:
    print("Givem String is empty")
else:
    print("Given String is not empty")

Output:

Given String is empty

Method #2:Using not

The not operator can do the same thing as len() and search for 0 length strings, but it also considers strings with only spaces to be non-empty, which isn’t realistic.

In Python, an empty string is equivalent to False. So, to see if a string is empty or not, we can use the “not” operator, .

Below is the implementation:

# given string
string = ""
#  determining whether or not the given string is empty
if not string:
    print("Given String is empty")
else:
    print("Given String is not empty")

Output:

Given String is empty

Check whether string is empty/contain spaces

Using strip()

To get a copy of a string without leading and trailing white spaces, we can use the string’s strip() function. So, let’s use this to see if the string is empty or only contains white spaces.

Below is the implementation:

# given string
string = " "
#  determining whether or not the given string is empty
if string and string.strip():
print("Given String is not empty")
else:
print("Given String is empty")

Output:

Given String is empty

Using isspace()

The string class’s isspace() function returns True if the string only contains white spaces. So we can use this to see if a string is empty or only contains white spaces.

To get a copy of a string without leading and trailing white spaces, we can use the string’s strip() function. So, let’s use this to see if the string is empty or only contains white spaces.

Below is the implementation:

# given string
string = "      "
#  determining whether or not the given string is empty
if string and not string.isspace():
    print("Given String is not empty")
else:
    print("Given String is  empty")

Output:

Given String is empty

Related Programs: