Python check space string

How to check if text is “empty” (spaces, tabs, newlines) in Python?

The first method is to use the built-in string library’s isspace() method. The isspace() method determines whether a string contains only spaces or contains any other characters.

If the given string is only made up of spaces, this method returns true; otherwise, it returns false. Even if the string contains characters like t and n, the method returns true.

Example

In the program given below, we are taking 3 different strings and finding out if they contain only spaces using the isspace() method −

str1 = " " str2 = " DAS" str3 = "\n\t" print("Checking whether the given string'",str1,"'contains only spaces") print(str1.isspace()) print("Checking whether the given string'",str2,"'contains only spaces") print(str2.isspace()) print("Checking whether the given string'",str3,"'contains only spaces") print(str3.isspace())

Output

The output to the above-given example is as shown below −

Checking whether the given string' 'contains only spaces True Checking whether the given string' DAS 'contains only spaces False Checking whether the given string' 'contains only spaces True

Using Regular Expressions

The second method involves the use of Regular expressions. The search method of the re library uses the regular expression «s* $ ,» which returns true if the string contains only spaces and false if the string contains any other characters.

Example 1

In the program given below, we are taking 3 different strings and finding out if they contain only spaces using the Regular expression “^\s* $ ” and search method of re library −

import re str1 = " " str2 = " DAS" str3 = "\n\t" print("Checking whether the given string'",str1,"'contains only spaces") if not str1 or re.search("^\s*$", str1): print('true') else: print('false') print("Checking whether the given string'",str2,"'contains only spaces") if not str2 or re.search("^\s*$", str2): print('true') else: print('false') print("Checking whether the given string'",str3,"'contains only spaces") if not str3 or re.search("^\s*$", str3): print('true') else: print('false')

Output

The output of the above example is given below−

Checking whether the given string' 'contains only spaces true Checking whether the given string' DAS 'contains only spaces false Checking whether the given string' 'contains only spaces true

Example 2

For matching only whitespace, we can also call the re.match(regex, string) using the regex metacharacter \s as follows: «^\s* $ » −

import re print(bool(re.match('^\s+$', ' abc'))) print(bool(re.match('^\s+$', ' ')))

Output

Following is an output of the above code −

Using len() function

The third option is to make use of the built−in len() function. The length of the given string must be determined. If the string’s length is 0, it means the string is empty or only contains whitespace letters; otherwise, the string contains other characters.

Читайте также:  Алгоритм сортировки расческой python

Example

In the example given below, we are checking if a given string is empty or not by using the len() method.

str1 = " " str2 = " DAS" str3 = "" print("Checking whether the given string'",str1,"'is empty") if len(str1) == 0: print('true') else: print('false') print("Checking whether the given string'",str2,"'is empty") if len(str2) == 0: print('true') else: print('false') print("Checking whether the given string'",str3,"'is empty") if len(str3) == 0: print('true') else: print('false')

Output

The output of the above example is given below −

Checking whether the given string' 'is empty false Checking whether the given string' DAS 'is empty false Checking whether the given string' 'is empty true

Using strip() function

The fourth option is to make use of the built-in strip() function. The strip() function removes all of the unnecessary spaces from the string. To determine whether a string contains only empty spaces, compare the stripped string to the original string; if they are identical, the string contains only white spaces or is empty.

Example

In the program, given below we are checking if a given string contains only spaces or not using the strip() method and comparing it with the actual string.

import re str1 = " " str2 = " DAS" str3 = "" print("Checking whether the given string'",str1,"'contains only spaces") if str1 and str1.strip(): print('false') else: print('true') print("Checking whether the given string'",str2,"'contains only spaces") if str2 and str2.strip(): print('false') else: print('true') print("Checking whether the given string'",str3,"'contains only spaces") if str3 and str3.strip(): print('false') else: print('true')

Output

The output of the above example is as shown below −

Checking whether the given string' 'contains only spaces true Checking whether the given string' DAS 'contains only spaces false Checking whether the given string' 'contains only spaces true

Источник

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,

Читайте также:  Which companies use python

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

Источник

Оцените статью