首页 > 编程知识 正文

Python isspace()

时间:2023-11-24 15:27:52 阅读:308739 作者:OWKE

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

 **string.isspace()** 

isspace()参数:

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

isspace()返回值

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

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

Python 中isspace()方法的示例

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

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

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

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

输出:

 True
False
False 

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

 string = 't  n'
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 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。