How to check for spaces in Python string?


In the current 21st century, handling data is the most challenging task for organizations with a high volume of data and with the development of data science and machine learning it has become easier to access. The spaces are also known as the whitespaces in between the characters of strings. The space has no data or it is simply empty and it creates a problem for programmers while coding. So, in order to check for spaces in the string involves many methods and some are explained here. Strings are composed of characters and Python Language comes under the OOPS concept and it runs the code immediately without checking for errors.

To Check for Spaces in Python String

In the string data structure the spaces are checked with the help of various methods or approaches as given below,

Approaches

Approach 1: Using isspace() method

Approach 2: Using regular expression

Approach 3: Using ‘in’ operator

Approach 1: Python Program to check for spaces in a string using isspace() method

The isspace() method checks the whitespace in the given string and the index value is mentioned to check for the spaces.

Algorithm

  • Step 1 − Initialize the variable named check_sent.

  • Step 2 − The strings are having whitespaces in between the words and to check the spaces isspace() method is used.

  • Step 3 − It verifies whether the whitespace is present at that index value or not based on the index value.

  • Step 4 − The index value begins from 0 and in this code index value 7 is initialized to search the spaces.

  • Step 5 − If the space would exist it returns “True” or else it will return “False”

  • Step 6 − On the basis of space, the respective statement is returned.

Example

#initializing the string with a sentence and assigning it to a variable
check_sent = "Welcome to Tutorialspoint"
#isspace() method is used to check whether the space is found in index value of 7
if check_sent[7].isspace():
   #if true this statement is printed
	print("The string contains whitespaces")
else:
   #if false this statement is printed
	print("The string does not contain whitespaces")

Output

The string contains whitespaces

Approach 2: Python Program to check for spaces in string using regular expression

Algorithm

  • Step 1 − Importing the re module and input is initialized with a string variable as " Welcome to Tutorialspoint".

  • Step 2 − The strings are having whitespaces in between the words and to check the spaces re.match() method is used.

  • Step 3 − Based on the index value given, it finds whether the whitespace is present at that index value or not.

  • Step 4 − The index value starts from 0 and in this code index value 7 is initialized to find the spaces.

  • Step 5 − If the space is present it returns “True” or else it will return “False”

  • Step 6 − Finally, print the specified statement.

Example

#importing the re module
import re

# initializing the string with a sentence and assigning it to a variable
check_sent = "Welcome to Tutorialspoint"

# using regular expressions to check if there is whitespace at index 7
if re.match(r"\s", check_sent[7]):
   #if true this statement is printed
   print("The string contains whitespace")
else:
   #if false this statement is printed
   print("The string does not contain whitespace")

Output

The string contains whitespaces

Approach 3: Python Program to check for spaces in string using ‘in’ operator

The ‘in’ operator is declared and based on this declaration, the whitespace is checked in the given string.

Algorithm

  • Step 1 − The input string is initialized and assigned a variable.

  • Step 2 − The strings are having whitespaces in between the words and to check the spaces ‘in’ operator is used.

  • Step 3 − Based on the index value given, it finds whether the whitespace is present at that index value or not.

  • Step 4 − The index value starts from 0 and in this code index value 7 is initialized to find the spaces.

  • Step 5 − If the space is present it returns “True” or else it will return “False”

  • Step 6 − On the basis of space, the respective statement is returned.

Example

# initializing the string with a sentence and assigning it to a variable
check_sent = "Welcome to Tutorialspoint"

# using the 'in' operator to check if there is whitespace at index 7
if ' ' in check_sent[7]:
   # if true this statement is printed
   print("The string contains whitespaces")
else:
   # if false this statement is printed
   print("The string does not contain whitespaces")

Output

The string does not contain whitespaces

Conclusion

The Python program is used to check for the spaces or whitespace in the given data structure. To check for spaces in the Python string we can use varying methods such as “in” operator, isspace() and also with regular expressions. After removing the spaces, the program runs smoothly.

Updated on: 25-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements