Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Check for spaces in string

Improve Article
Save Article
  • Last Updated : 24 Jan, 2023
Improve Article
Save Article

Sometimes, we might have a problem in which we need to check if the string has any of blank spaces. This kind of problem can be in Machine Learning domain to get specific type of data set. Let’s discuss certain ways in which this kind of problem can be solved. 

Method #1 : Using regex This kind of problem can be solved using the regex utility offered by python. By feeding the appropriate regex string in search(), we can check presence of space in a string. 

Python3




# Python3 code to demonstrate working of
# Check for spaces in string
# Using regex
import re
 
# initializing string
test_str = "Geeks  forGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using regex
# Check for spaces
res = bool(re.search(r"\s", test_str))
 
# printing result
print("Does string contain spaces ? " + str(res))

Output : 

The original string is : Geeks  forGeeks
Does string contain spaces ? True

Method #2 : Using in operator This task can also be performed using in operator. Just required to check for a space in the string. The verdict returned is true even if a single space is found and false otherwise. 

Python3




# Python3 code to demonstrate working of
# Check for spaces in string
# Using in operator
 
# initializing string
test_str = "Geeks  forGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using in operator
# Check for spaces
res = " " in test_str
 
# printing result
print("Does string contain spaces ? " + str(res))

Output : 

The original string is : Geeks  forGeeks
Does string contain spaces ? True

Method #3: Using find() method.

find() method searches the given string for argument passed and returns the position or else returns -1.

Python3




# Python3 code to demonstrate working of
# Check for spaces in string
 
 
# initializing string
test_str = "Geeks forGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using in operator
# Check for spaces
res=False
if(test_str.find(" ")!=-1):
    res=True
 
# printing result
print("Does string contain spaces ? " + str(res))

Output

The original string is : Geeks forGeeks
Does string contain spaces ? True

Method #4: Using isspace() method
 

Python3




# Python3 code to demonstrate working of
# Check for spaces in string
 
# initializing string
test_str = "Geeks forGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Check for spaces
c=0
res=False
for i in test_str:
    if(i.isspace()):
        c+=1
if(c>=1):
    res=True
 
# printing result
print("Does string contain spaces ? " + str(res))

Output

The original string is : Geeks forGeeks
Does string contain spaces ? True

Method #5: Using count() method:

The count() method is a built-in method in Python that returns the number of occurrences of a specified substring in a string. Here is an example of how to use the count() method to check if a string contains any spaces:
 

Python3




# Python3 code to demonstrate working of
# Check for spaces in string
# Using count() method
 
# initializing string
test_str = "Geeks forGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using count() method
# Check for spaces
res = False
if test_str.count(" ") > 0:
    res = True
 
# printing result
print("Does string contain spaces ? " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original string is : Geeks forGeeks
Does string contain spaces ? True

In this code, we first initialize a string called “test_str” which contains the text we want to check for spaces. Next, we call the count() method on the string and pass in ” ” (a single space) as the argument.
The count() method returns the number of occurrences of the specified substring in the string.
We check if the number of spaces greater than zero by using if statement, and if it is true then we set the variable res as true.
Finally, we print the result of whether or not the string contains spaces.

Time complexity: O(n)
Auxiliary Space: O(1)

Method #6: Using operator.countOf() method:

Python3




# Python3 code to demonstrate working of
# Check for spaces in string
import operator as op
 
# initializing string
test_str = "Geeks forGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using count() method
# Check for spaces
res = False
if op.countOf(test_str, " ") > 0:
    res = True
 
 
print("Does string contain spaces ? " + str(res))
# This code is contributed by vikkycirus

Output

The original string is : Geeks forGeeks
Does string contain spaces ? True

Time complexity: O(n)
Auxiliary Space: O(1)

Method#7:Using Recursion

Python3




# Python3 code to demonstrate working of
# Check for spaces in string
# Using Recursion
def ContainSpace(st,i):
  if i == len(st):
    return False
  if st[i] == " ":
    return True
  return ContainSpace(st,i+1)
# initializing string
test_str = "Geeks forGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
 
res = ContainSpace(test_str,0)
# printing result
print("Does string contain spaces ? " + str(res))
 
#This code is contributed by vinay Pinjala.

Output

The original string is : Geeks forGeeks
Does string contain spaces ? True

Time complexity: O(n)
Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!