Python isspace()方法用于检查字符串中的空格。如果字符串中仅包含空格字符,则返回true。否则,它返回false。空格,换行符和制表符等称为空白字符,并且在Unicode字符数据库中定义为 Other或Separator 。
isspace()
它返回True或False。
链接:https://www.learnfk.comhttps://www.learnfk.com/python3/python-string-isspace-method.html
来源:LearnFk无涯教程网
让无涯教程看一下isspace()方法的一些例子,以了解它的功能。
# Python isspace() method example # 变量声明 str = " " # empty string # 函数调用 str2 = str.isspace() # 显示结果 print(str2)
输出:
True
# Python isspace() method example # 变量声明 str = "ab cd ef" # string contains spaces # 函数调用 str2 = str.isspace() # 显示结果 print(str2)
输出:
False
isspace()方法对所有空白都返回true,例如:
# Python isspace() method example # 变量声明 str = " " # string contains space if str.isspace() == True: print("It contains space") else: print("Not space") str = "ab cd ef \n" if str.isspace() == True: print("It contains space") else: print("Not space") str = "\t \r \n" if str.isspace() == True: print("It contains space") else: print("Not space")
输出:
It contains space Not space It contains space
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)