What is isspace () in python?

Characters that are used for spacing are called whitespace characters. For example: tabs, spaces, newline, etc.

The syntax of isspace() is:

string.isspace()

isspace() Parameters

isspace() method doesn't take any parameters.


Return Value from isspace()

isspace() method returns:

  • True if all characters in the string are whitespace characters
  • False if the string is empty or contains at least one non-printable character

Example 1: Working of isspace()

s = '   \t'
print(s.isspace())

s = ' a '
print(s.isspace())

s = ''
print(s.isspace())

Output

True
False
False

Example 2: How to use isspace()?

s = '\t  \n'
if s.isspace() == True:
  print('All whitespace characters')
else:
  print('Contains non-whitespace characters')
  
s = '2+2 = 4'

if s.isspace() == True:
  print('All whitespace characters')
else:
  print('Contains non-whitespace characters.')

Output

All whitespace characters
Contains non-whitespace characters

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

    Python isspace() method is used to check space in the string. It returna true if there are only whitespace characters in the string. Otherwise it returns false. Space, newline, and tabs etc are known as whitespace characters and are defined in the Unicode character database as Other or Separator.

    Signature

    Parameters

    No parameter is required.

    Return

    It returns either True or False.

    Let's see some examples of isspace() method to understand it's functionalities.

    Python String isspace() Method Example 1

    Output:

    Python String isspace() Method Example 2

    Output:

    Python String isspace() Method Example 3

    isspace() method returns true for all whitespaces like:

    • ' ' - Space
    • '\t' - Horizontal tab
    • '\n' - Newline
    • '\v' - Vertical tab
    • '\f' - Feed
    • '\r' - Carriage return

    Output:

    It contains space
    Not space
    It contains space
    


    How do I use Isspace?

    The isspace() function checks whether a character is a white-space character or not. If an argument (character) passed to the isspace() function is a white-space character, it returns non-zero integer. ... Function prototype of isspace().

    How do you show spaces in Python?

    Python isspace() method is used to check space in the string. It returna true if there are only whitespace characters in the string. Otherwise it returns false. Space, newline, and tabs etc are known as whitespace characters and are defined in the Unicode character database as Other or Separator.

    What is whitespace in Python example?

    Q2. What are whitespace characters in Python? In Python, characters that are used for spacing are called as whitespace characters. They include newline, spaces, tabs, carriage return, feed, etc.

    How do I remove spaces from a string in Python?

    Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.