Methods and Functions

Python String isspace()

We frequently need to determine whether a string simply contains whitespaces or not. It would be extremely helpful and time-saving if we could check this problem using an in-built function. The Python isspace() function can address this problem in just two lines of code. When we just have spacing characters as input, the Python isspace() function comes in handy. Let us understand more about the Python isspace() string class method, its syntax, and its applications and examples.

Python isspace() function

Definition

  • The Python isspace() String function examines a string for whitespace characters and returns True only if the string contains all spacing characters, i.e. different types of spaces (\n, \t, \r, \v, ‘ ‘); else it returns False.
  • Python isspace() is a built-in method that returns True if all of the characters in the string are spacing characters (space, horizontal tab, vertical tab, newline, etc.). If not, it returns False.

Python isspace()

The Python isspace() function returns a Boolean value depending on the existence of spacing characters in the string. In Python, characters that are used for spacing are called as whitespace characters. They include newline, spaces, tabs, carriage return, feed, etc.

The Python String isspace() method is used to determine whether an argument has all whitespace characters such as:

  1. ‘ ‘ – Space
  2. ‘\t’ – Horizontal tab
  3. ‘\v’ – Vertical tab
  4. ‘\n’ – Newline
  5. ‘\r’ – Carriage return
  6. ‘\f’ – Feed

isspace() Syntax

The syntax followed by Python isspace() is as follows:

                    

string.isspace()

isspace() Parameters

Python String isspace() function does not accept any parameters. If any parameters are passed, this function throws an exception.

Return value of isspace()

The isspace() is responsible for returning a Boolean value:

  • True – if there exists all/only whitespace characters in the string input
  • False – if the string contains at least one character which is not a whitespace character.

Note – If the string is empty without any spacing, then the isspace() function returns a False value.

Example 1: Working of isspace()

Example

                    

# Python program to illustrate isspace()
txt = ' '
print(txt.isspace())

txt = '    '
print(txt.isspace())

txt = '\n \t'
print(txt.isspace())

txt = '\r\t\n'
print(txt.isspace())

Output

                    

True
True
True
True

In the above example, all the input strings are entirely made up of white spaces. As a result, the isspace() method returns True.

Example 2: Working of isspace()

Even if one character is not a whitespace, the isspace() method will return False, as seen below.

Example

                    

# Python program to illustrate isspace()
txt = 'Welcome_Back'
print(txt.isspace())

txt = 'Hello World'
print(txt.isspace())

txt = '\nHello \tPython'
print(txt.isspace())

txt = '$@\n'
print(txt.isspace())

Output

                    

False
False
False
False

One of the string inputs in the above program consists of a combination of whitespace and non-whitespace characters. For this situation also, the isspace() function returns a False value.

Example 3: Unicode Whitespace Characters

Python isspace() also supports Unicode characters which qualify as whitespace characters in Python. For example:

                    

txt = '\u0020'
print(txt.isspace())

txt = '\u00A0'
print(txt.isspace())

txt = '\u2005 \u2007'
print(txt.isspace())

Output

                    

True
True
True

The list of whitespace characters in Unicode is as below:

Unicode Character Description
\u0020 Space
\u00A0 No-Break Space
\u1680 Ogham Space Mark
\u2000 En Quad
\u2001 Em Quad
\u2002 En Space
\u2003 Em Space
\u2004 Three-per-em Space
\u2005 Four-per-em Space
\u2006 Six-per-em Space
\u2007 Figure Space
\u2008 Punctuation Space
\u2009 Thin Space
\u200A Hair Space
\u202F Narrow No-Break Space
\u205F Medium Mathematical Space
\u3000 Ideographic Space

Example 4: How to use isspace()?

One real-life application where the isspace() function can be used is to find out the number of whitespace characters in the input string.

Example

                    

string = 'Hello World\n\n Python Programming\t'
count = 0
for i in string:
    if(i.isspace()) == True:
        count += 1
print('Number of spaces in the string are:', count)

Output

                    

Number of spaces in the string are: 6

Frequently Asked Questions

Q1. What is isspace in Python?

We frequently need to determine whether a string simply contains whitespaces or not. It would be extremely helpful and time-saving if we could check this problem using an in-built function. The Python isspace() String function examines a string for whitespace characters and returns True only if the string contains all spacing characters, i.e. different types of spaces (\n, \t, \r, \v, ‘ ‘); else it returns False.

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.

Whitespace is a string that has been pre-initialized and is used as a string constant. Whitespace is simply a character that is used for spacing and has an “empty” representation. It refers to tabs and spaces in the context of Python (it also includes exotic Unicode spaces).

The Python String isspace() method is used to determine whether an argument has all whitespace characters such as:

  1. ‘ ‘ – Space
  2. ‘\t’ – Horizontal tab
  3. ‘\v’ – Vertical tab
  4. ‘\n’ – Newline
  5. ‘\r’ – Carriage return
  6. ‘\f’ – Feed

Q3. How do you find whitespace in Python?

The syntax followed by Python isspace() is as follows:

                    

string.isspace()

Python String isspace() function does not accept any parameters. If any parameters are passed, this function throws an exception.

Example

                    

txt = '    '
print(txt.isspace())

txt = 'Python rocks'
print(txt.isspace())

txt = '\nProgramming\n\t'
print(txt.isspace())

txt = '\n  \t  \r'
print(txt.isspace())

Output

                    

True
False
False
True

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.