常用类型方法 2.1

目录

四、掌握数据的验证

五、掌握数据的处理

a.字符串拼接的几种方式

b.字符串去重操作

六、掌握正则表达式的使用

a.元字符:

b.限定符:

c.re模块:

总结:


四、掌握数据的验证

数据的验证是指程序对用户输入的数据进行“合法性”验证

方法名描述说明
str.isdingit()所有字符都是数字(阿拉伯数字)
str.isnumeric()所有字符都是数字
str.isalpha()所有字符都是字母(包含中文字符)
str.isalnum()所有字母都是数字或字母(包含中文字符)
str.islower()所有字符都是小写
str.isupper()所有字符都是大写
str.istitle()所有字符都是首字母大写
str.isspace()所有字符都是空白字符(\n \t等)

五、掌握数据的处理

a.字符串拼接的几种方式

  1. 使用str.join()方法进行拼接字符串
  2. 直接拼接
  3. 使用格式化字符串进行拼接
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']

总结:

  • 字符串的常用方法:
  1. 大小写转换方法:str.lower()、str.upper()
  2. 字符串分隔方法:str.split()
  3. 检索的方法:str.count()、str.find()、str.index()
  4. 字符串判断的方法:str.startswith()、str.endswith()
  5. 字符串替换的方法:str.replace()
  6. 字符串显示方式的方法:str.center()
  7. 字符串拼接方法:str.join()
  8. 去除字符串前后字符的方法:str.strip()、str.lstrip()、str.rstrip()
  • 格式化字符串的三种方式:
  1. 使用占位符进行格式化字符串
  2. f-string格式化字符串
  3. 使用字符串的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()
  • 36
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FANGNG20

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值