Python---字符串第二篇
1.find
字符串从头开始找,找到第一个符合的子字符串获取位置就结束。
find(self, sub, start=None, end=None)
test = "leslieleslie"
v = test.find('es')
print(v)
结果为1
test = "leslieleslie"
v = test.find('es',4,9)
print(v)
结果为7
2.isalnum
判断字符串中是否只包含数字和字母。
test = "asdf134"
v = test.isalnum()
print(v)
结果为True
3.isalpha
检测字符串是否只由字母组成。
test = "asdf"
v = test.isalpha()
print(v)
结果为True
4.isdecimal,isdigit,isnumeric
isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
注意:定义一个十进制字符串,只需要在字符串前添加 'u' 前缀即可。
isdigit() 方法检测字符串是否只由数字组成。
isnumeric还可以判断中文数字
注意点:
1.python官方定义中的字母:大家默认为英文字母+汉字即可
2.python官方定义中的数字:大家默认为阿拉伯数字+带圈的数字即可
test = "123" v1 = test.isdecimal() v2= test.isdigit()
print(v1,v2) 结果为True True
test = "②23"
v1 = test.isdecimal()
v2= test.isdigit()
print(v1,v2)
结果为False True
test = "1二三"
v1 = test.isdecimal()
v2= test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
结果为False False True
5.isidentifier
判断字符串是否是有效的 Python 标识符,可用来判断变量名是否合法。
test = "def"
v = test.isidentifier()
print(v)
结果为True
6.expendtabs
字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8。
进行断句,根据后面的参数进行判断,如果字符串满足条件则不需要补空格,否则由空格进行
填充。
test = "username\temail\tpassword\nyyy\tyyy@qq.com\t111\nrrr\trrr@qq.com\t222\nsss\tsss@qq.com\t333\n"
v = test.expandtabs(20)
print(v)
结果为
username email password
yyy yyy@qq.com 111
rrr rrr@qq.com 222
sss sss@qq.com 333

浙公网安备 33010602011771号