English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
If the string contains only space characters, the isspace() method will return True. Otherwise, it returns False.
Characters used for spacing are called whitespace characters. For example: tab, space, newline, etc.
The syntax of isspace() is:
string.isspace()
isspace() method without any parameters.
isspace() method returns:
True if all characters in the string are space characters
False if the string is empty or contains at least one non-Printable() characters
s = ' \t' print(s.isspace()) s = ' a ' print(s.isspace()) s = '' print(s.isspace())
When running the program, the output is:
True False False
s = '\t \n' if s.isspace() == True: print('All space characters') else: print('Contains non-space characters') s = '2+2 = 4' if s.isspace() == True: print('All space characters') else: print('Contains non-space characters')
When running the program, the output is:
All space characters Contains non-space characters