Python String isspace

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

String_Value.isspace()

Before we get into the examples of Python isspace function, let me show you the whitespace characters

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

Python String isspace 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 function returns True only if all the characters are empty spaces. That’s why you see False.

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

String isspace Example 2

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 isspace String function 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