判断
函数 | 含义 |
---|
startswith() | 判断指定字符、字符串、元组是否出现在字符串的开始位置 | endswith() | 判断指定字符、字符串、元组是否出现在字符串的结束位置 | istitle() | 判断是否字符串中每个开头都为大写,其他为小写 | isupper() | 判断是否都为大写 | islower() | 判断是否都为小写 | isalpha() | 判断是否都为字母 | isspace() | 判断是否为空白字符串 | isprintable() | 判断是否存在转义字符 | isdecimal() | 判断是否为数字 | isdigit() | 判断是否为数字 | isnumeris() | 判断是否为数字 | isalnum() | 判断字符串是否由字母和数字构成 | isidentifier() | 判断字符串是否为合法字符串 |
startswith()
startswith(“a”)
x = "hello world"
print(x.startswith("h"))
print(x.startswith("H"))
print(x.startswith("hello"))
startswith(“a”,start,end)
x = "hello world"
print(x.startswith("e",1))
print(x.startswith("ello",1))
startswith((“a”,“b”,“c”),start,end) 元组
startswith不仅仅可以判断字符、字符串,也可以判断元组
x = "hello world"
if x.startswith(("a","b","h")):
print("yes")
else:
print("no")
endswith()
类比startswith,参数相同,只是一个判断开头一个判断结尾。
endswith(“a”) & endswith(“a”,start,end)
x = "hello world"
print(x.endswith("d"))
print(x.endswith("world"))
print(x.endswith("worl",0,-1))
print(x[-2])
istitle()
判断字符串是否为大小字母开头,其他字符为小写字符
x = "Hello World"
print(x.istitle())
x = "HEllo World"
print(x.istitle())
x = "Hello world"
print(x.istitle())
isupper()
判断字符串中是否所有字符都是大写
x = "Hello World"
print(x.isupper())
x = "HELLO WORLD"
print(x.isupper())
x = "HELLO WORLd"
print(x.isupper())
islower()
类比isupper(),islower()判断是否字符串中所有字符都为小写字符
x = "hello world"
print(x.islower())
x = "hello World"
print(x.islower())
x = "HELLO world"
print(x.islower())
isalpha()
判断字符串中是不是都是字母
x = "hello world"
print(x.isalpha())
x = "helloworld"
print(x.isalpha())
x = "hello1111"
print(x.isalpha())
第一个是 Flase 的原因是 hello 和 world 中间出现空格非字母
isspace()
判断字符串是否为空白字符串
x = " \n\t"
print(x.isspace())
isprintable()
判断是否可以打印,主要是判断是否存在转义字符
x = "hello world"
print(x.isprintable())
x = "hello world\n"
print(x.isprintable())
isdecimal & isdigit & isnumeric
判断是否为数字,但是可以判断的范围不同
x = "12345"
print(x.isdecimal())
print(x.isdigit())
print(x.isnumeric())
x = "22"
print(x.isdecimal())
print(x.isdigit())
print(x.isnumeric())
x = "Ⅰ"
print(x.isdecimal())
print(x.isdigit())
print(x.isnumeric())
x = "一二三四五"
print(x.isdecimal())
print(x.isdigit())
print(x.isnumeric())
isalnum()
集大成者,只要isalpha、isdecimal、isdigit、isnumeric中有一个为true则为true
isidentifier()
判断字符串是否为合法的标识符 合法:不可以数字开头,不存在空格…
x = "hello world"
print(x.isidentifier())
x = "hello_world"
print(x.isidentifier())
x = "520hello"
print(x.isidentifier())
判断字符是否为python保留标识符(if while for …)
import keyword
print(keyword.iskeyword("if"))
print(keyword.iskeyword("while"))
print(keyword.iskeyword("array"))
|