Find space in string python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    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

    import re

    test_str = "Geeks  forGeeks"

    print("The original string is : " + test_str)

    res = bool(re.search(r"\s", test_str))

    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

    test_str = "Geeks  forGeeks"

    print("The original string is : " + test_str)

    res = " " in test_str

    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

    test_str = "Geeks forGeeks"

    print("The original string is : " + test_str)

    res=False

    if(test_str.find(" ")!=-1):

        res=True

    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

    test_str = "Geeks forGeeks"

    print("The original string is : " + test_str)

    c=0

    res=False

    for i in test_str:

        if(i.isspace()):

            c+=1

    if(c>=1):

        res=True

    print("Does string contain spaces ? " + str(res))

    Output

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


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python String isspace() method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as:

    • ‘ ‘ – Space
    • ‘\t’ – Horizontal tab
    • ‘\n’ – Newline
    • ‘\v’ – Vertical tab
    • ‘\f’ – Feed
    • ‘\r’ – Carriage return

    Python String isspace() Method Syntax

    Syntax: string.isspace()

    Returns:

    1. True – If all characters in the string are whitespace characters.
    2. False – If the string contains 1 or more non-whitespace characters.

    Python String isspace() Method Example

    Python3

    string = "\n\t\n"

    print(string.isspace())

    Output:

    True

    Example 1:  Basic Intuition of isspace() in Program

    Here we will check whitespace in the string using isspace() program.

    Python3

    string = 'Geeksforgeeks'

    print(string.isspace())

    string = '\n \n \n'

    print(string.isspace())

    string = 'Geeks\nfor\ngeeks'

    print( string.isspace())

    Output: 

    False
    True
    False

    Example 2: Practical Application

    Given a string in Python, count the number of whitespace characters in the string. 

    Input : string = 'My name is Ayush'
    Output : 3
    
    Input : string = 'My name is \n\n\n\n\nAyush'
    Output : 8

    Algorithm: 

    1. Traverse the given string character by character up to its length, check if the character is a whitespace character. 
    2. If it is a whitespace character, increment the counter by 1, else traverse to the next character. 
    3. Print the value of the counter.

    Python3

    string = 'My name is Ayush'

    count=0

    for a in string:

        if (a.isspace()) == True:

            count+=1

    print(count)

    string = 'My name is \n\n\n\n\nAyush'

    count = 0

    for a in string:

        if (a.isspace()) == True:

            count+=1

    print(count)

    Output: 

    3
    8

    What is Isspace () in Python?

    The isspace() method returns True if all the characters in a string are whitespaces, otherwise False.

    How do you split a string with spaces in Python?

    The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

    Is space a string?

    Yes, space is a character. Usually, programmers use "character" to refer to "some individual and indivisible unit of text." When you're writing out a string or some sequence of text, then you somehow need to mark where in that text the spaces occur.