Python isspace Function

The Python isspace function returns True if all the characters in a given string are whitespace characters. Otherwise, it returns False and the syntax of this is

String_Value.isspace()

Python isspace function whitespace characters

  • ‘ ‘ – An empty space
  • \n – New Line
  • \t – Horizontal Tab
  • \v – Vertical Tab
  • \f – feed
  • \r – Carriage return

Python isspace String Example

In this example, we show how to find or check spaces in a string using this isspace function. The first string is an empty string, but there is no empty space. The second statement has an empty space. Within the third statement, we used a word with a combination of spaces.

As you know, this Python method returns True only if all the characters are empty spaces. That’s why you see the False as the output.

str1 = ''
print(str1.isspace())
 
str2 = ' '
print(str2.isspace())
 
str3 = '  Python   '
print(str3.isspace())
False
True
False

In this example, we are using the Python isspace function against the Newline, horizontal tab, etc. These statements always return True.

However, if you use them along with text, then the String method returns false.

str1 = '\n'
print(str1.isspace())
 
str2 = '\t'
print(str2.isspace())
 
str3 = '\v'
print(str3.isspace())
 
str4 = '\f'
print(str4.isspace())
 
str5 = '\r'
print(str5.isspace())
 
str6 = '\n \t \v'
print(str6.isspace())
Python isspace 2