Use the isspace()
method to check if the characters in a string are all white spaces.
text = ' '
print(text.isspace())
#output: True
text = ' \f\n\r\t\v'
print(text.isspace())
#output: True
text = ' '
print(text.isspace())
#output: True
text = '' # notice this is an empty string, there is no white space here
print(text.isspace())
#output: False
text = 'This is a regular text'
print(text.isspace())
#output: False
Notice in the second example that white space is not only ' '
, but also form feed \f
, line feed \n
, carriage return \r
, tab \t
, and vertical tab \v
.