目录
四、掌握数据的验证
数据的验证是指程序对用户输入的数据进行“合法性”验证
方法名 | 描述说明 |
---|---|
str.isdingit() | 所有字符都是数字(阿拉伯数字) |
str.isnumeric() | 所有字符都是数字 |
str.isalpha() | 所有字符都是字母(包含中文字符) |
str.isalnum() | 所有字母都是数字或字母(包含中文字符) |
str.islower() | 所有字符都是小写 |
str.isupper() | 所有字符都是大写 |
str.istitle() | 所有字符都是首字母大写 |
str.isspace() | 所有字符都是空白字符(\n \t等) |
五、掌握数据的处理
a.字符串拼接的几种方式
- 使用str.join()方法进行拼接字符串
- 直接拼接
- 使用格式化字符串进行拼接
s1='hello'
s2='world'
# (1)使用+进行拼接
print(s1+s2)
#(2) 使用字符串的join()方法
print(''.join([s1,s2])) # 使用空字符串进行拼接
print('*'.join(['hello','world','python','java','php']))
# (3) 直接拼接
print('hello''world')
# (4)使用格式化字符串进行拼接
print('%s%s'%(s1,s2))
print(f'{s1}{s2}')
print('{0}{1}'.format(s1,s2))
b.字符串去重操作
s='helloworldhelloworldhelloworldhelloworldhelloworldff'
# (1) 字符串拼接及not in
new_s=''
for item in s:
if item not in new_s:
new_s+=item #拼接操作
print(new_s)
# (2) 使用索引+not in
new_s2=''
for i in range(len(s)):
if s[i] not in new_s2:
new_s2+=s[i]
print(new_s2)
# (3) 通过集合去重+列表排序
new_s3=set(s)
lst=list(new_s3)
lst.sort(key=s.index)
print(''.join(lst))
六、掌握正则表达式的使用
a.元字符:
具有特殊意义的专用字符;例如"^""$"分别表示匹配的开始和结束
元字符 | 描述说明 | 举例 | 结果 |
---|---|---|---|
. | 匹配任意字符(除\n) | 'p\nytho\tn' | p、y、t、h、o、\t、n |
\w | 匹配字母、数字、下划线 | 'python\n123' | p、y、t、h、o、n、1、2、3 |
\W | 匹配非字母、数字、下划线 | 'python\n123' | \n |
\s | 匹配任意空白字符 | 'python\t123' | \t |
\S | 匹配任意非空白字符 | 'python\t123' | p、y、t、h、o、n、1、2、3 |
\d | 匹配任意十进制数 | 'python\t123' | 1、2、3 |
b.限定符:
用于限定匹配的次数
限定符 | 描述说明 | 举例 | 结果 |
---|---|---|---|
? | 匹配前面的字符0次或1次 | colou?r | 可以匹配color或colour |
+ | 匹配前面的字符1次或多次 | colou+r | 可以匹配colour或colouu...r |
* | 匹配前面的字符0次或多次 | colou*r | 可以匹配color或colouu...r |
{n} | 匹配前面的字符n次 | colou{2}r | 可以匹配colouur |
{n,} | 匹配前面的字符最少n次 | colou{2,}r | 可以匹配colouur或colouuu |
{n,m} | 匹配前面的字符最小n次,最多m次 | colou{2,4}r | 可以匹配colouur或colouuur或colouuuur |
其他字符 | 描述说明 | 举例 | 结果 |
---|---|---|---|
区间字符[] | 匹配[]中所指定的字符 | [.?!] | 匹配标点符号点、问号、感叹号 匹配0、1、2、3、4、5、6、7、8、9 |
排除字符^ | 匹配不在[]中所指定的字符 | [^0-9] | 匹配除0、1、2、3、4、5、6、7、8、9的字符 |
选择字符| | 用于匹配|左右的任意字符 | \d{18}|\d{15} | 匹配15位身份证或18位身份证 |
转义字符 | 同python中的转义字符 | \. | 将.作为普通字符使用 |
[\u4e00-\u9fa5] | 匹配任意一个汉字 | ||
分组() | 改变限定符的作用 | six|fourth(six|four)th | 匹配six或fourth匹配sixth或fourth |
c.re模块:
python中的内置模块,用于实现python中的正则表达式操作
函数 | 功能描述 |
---|---|
re.match(pattern,string,flages=0) | 用于从字符串的开始位置进行匹配,如果起始位置匹配成功,结果位March对象,否则结果为None。 |
re.search(pattern,string,flages=0) | 用于在整个字符串中搜索第一个匹配的值,如果匹配成功,结果为Match对象,否则结果为None。 |
re.findall(pattern,string,flages=0) | 用于在整个字符串搜索所有符号正则表达式的值,结果是一个列表类型。 |
re.sub(pattern,repl,string,count,flages=0) | 用于实现对字符串中指定子串的替换 |
re.split(pattern,string,maxsplit,flages=0) | 字符串中的split()方法功能相同,都是分隔字符串 |
import re #导入
pattern='\d\.\d+' # +限定符,\d 0-9数字出现1次或多次
s='I study python 3.11 every day' # 待匹配字符串
match=re.match(pattern,s,re.I)
print(match) # None
s2='3.11python I study every day'
match2=re.match(pattern,s2)
print(match2) # <re.Match object; span=(0, 4), match='3.11'>
print('匹配值的起始位置:',match2.start())
print('匹配值的结束位置:',match2.end())
print('匹配区间的位置元素:',match2.span())
print('待匹配的字符串:',match2.string)
print('匹配的数据:',match2.group())
import re #导入
pattern='\d\.\d+' # +限定符,\d 0-9数字出现1次或多次
s='I study python 3.11 every day,python614study'
match=re.search(pattern,s)
s2='6.14 python I study every day'
match2=re.search(pattern,s2)
s3='I study every day'
match3=re.search(pattern,s3)
print(match) # <re.Match object; span=(15, 19), match='3.11'>
print(match2) # <re.Match object; span=(0, 4), match='6.14'>
print(match3) # None
print(match.group())
print(match2.group())
import re #导入
pattern='\d\.\d+' # +限定符,\d 0-9数字出现1次或多次
s='I study python 3.11 every day,python 614 study'
s2='6.14 python I study every day'
s3='I study python every day'
lst=re.findall(pattern,s)
lst2=re.findall(pattern,s2)
lst3=re.findall(pattern,s3)
print(lst) # ['3.11']
print(lst2) # ['6.14']
print(lst3) # []
import re #导入
pattern='鸭屎山|土壤|茶树'
s='在茶树下养鸭子拉屎'
new_s=re.sub(pattern,'XXX',s)
print(new_s)
s2='https://cn.bing.com/search?pglt=169&q'
pattern2='[?|&]'
lst=re.split(pattern2,s2)
print(lst) # ['https://cn.bing.com/search', 'pglt=169', 'q']
总结:
- 字符串的常用方法:
- 大小写转换方法:str.lower()、str.upper()
- 字符串分隔方法:str.split()
- 检索的方法:str.count()、str.find()、str.index()
- 字符串判断的方法:str.startswith()、str.endswith()
- 字符串替换的方法:str.replace()
- 字符串显示方式的方法:str.center()
- 字符串拼接方法:str.join()
- 去除字符串前后字符的方法:str.strip()、str.lstrip()、str.rstrip()
- 格式化字符串的三种方式:
- 使用占位符进行格式化字符串
- f-string格式化字符串
- 使用字符串的format方法进行格式化字符串
- 3. 字符串编码的方法:str.encode()
- 4. 字符串解码的方法:bytes.decode()
- 5. 数据验证的方法:str.isdingit()、str.isnumeric()、str.isalpha()、str.isalnum()、str.islower()、str.isupper()、str.istitle()、str.isspace()
- 6. 数据处理:字符串的拼接与去重
- 7. 内置模块re中的常用函数:re.match()、re.search()、re.findall()、re.sub()、re.split()