Python isspace()

python 中的isspace()函数有助于检查字符串中的所有字符是否都是空白字符。如果字符串中充满空白字符(制表符、空格、换行符等),则该函数返回 true。)否则返回 false。

 **string.isspace()** 

isspace()参数:

isspace()方法不接受任何参数。

isspace()返回值

返回值始终是布尔值。如果字符串为空,函数返回假。

| 投入 | 返回值 | | 所有空白字符 | 真实的 | | 至少一个非空白字符 | 错误的 |

Python 中isspace()方法的示例

示例isspace()在 Python 中是如何工作的?

 string = '   	'
print(string.isspace())

string = ' a '
print(string.isspace())

string = ''
print(string.isspace()) 

输出:

 True
False
False 

示例 2:如何在 Python 中使用isspace()

 string = '	  
'
if string.isspace() == True:
  print('String full of whitespace characters')
else:
  print('String contains non-whitespace characters')

string = '15+3 = 18'

if string.isspace() == True:
  print('String full of whitespace characters')
else:
  print('String contains non-whitespace characters.') 

输出:

 String full of whitespace characters
String contains non-whitespace characters 

你可能感兴趣的