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
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238856.html