评价一个网站的好坏,免费网络推广平台有哪些,2003 建设网站,王也台球字符串总结
python程序使用unicode编码#xff0c;中文字符与英文字符都占一个字符#xff0c;但英文字符只占一个字节#xff0c;中文字符若按照utf-8格式编码占3个字节。
#xff08;1#xff09;字符串常用方法
1#xff09;大小写转化
string.upper()#将所有字母…字符串总结
python程序使用unicode编码中文字符与英文字符都占一个字符但英文字符只占一个字节中文字符若按照utf-8格式编码占3个字节。
1字符串常用方法
1大小写转化
string.upper()#将所有字母转换为大写
string.lower()#将所有字母转换为小写
2字符串分割
string.split(sep)#将字符串按照sep进行分割
3字符串的检索
string.count(sub)#统计字符串中某个子串的出现次数
string.find(‘sub’)#查询字符串中是否有查询的子串有则返回第一次出现子串的索引没有则返回-1
string.index(sub)#与find类似区别是若字符串无该子串则程序报错
4字符串判断
string.startswith(sub)#判断字符串是否以查询的子串为开头
string.endswith(sub)#判断字符串是否以查询的子串结尾
5字符串替换
string.replace(sub1,sub2)#将字符串所有子串1替换为子串2
6字符串显示
string.center(width,fillchar)#在指定的范围内居中可指定填充字符默认为空格
7字符串拼接
string.join(iterable)#将可迭代的对象组(列表、元组、字符串等)合为一个字符串其中string为分隔符
8去除字符串前后字符
string.strip(sub)#去除字符串中头部与尾部指定的字串
string.lstrip(sub)#去除字符串中头部指定的字串
string.rstrip(sub)#去除字符串中尾部指定的字串
2格式化字符串
1使用占位符
%型%变量名变量名……其中中的变量是一个元组类型的数据
2f-string
(f{变量名} {变量名……}
3format
模板字符串.format(变量,变量……)
3字符串编码与解码
1字符串编码
string.encode(’编码格式‘’错误处理‘)#编码格式默认utf-8,错误处理包括严格strict(遇到无法编码的字符直接报错)、忽略ignore(遇到无法编码的字符直接跳过)、替换replace(遇到无法编码的字符使用字符替换默认是’‘)
2字符串解码
byte.decode(解码格式’错误处理‘)#解码格式与编码格式必须一致
4数据验证
1string.isdigit()#判断字符串是否全为阿拉伯数字
2string.isnumeric()#判断字符串是否全为数字包括罗马数字、中文的一二三四
3string.isalpha()#判断字符串是否全为字母或汉字
4string.isalnum()#判断字符串是否全为数字或字母包括中文字符
5string.islower()#判断字符串中的字母是否全为小写
6string.isupper()#判断字符串中的字母是否全为大写
7string.istitle()#判断字符串中的单词是否全为首字母大写
8string.isspace()#判断字符串是否都为空白、转义字符\t\n等
5字符串去重
有三种方式
1使用遍历
2使用索引遍历
3使用集合列表
6正则表达式
1元字符
具有特殊意义的专用字符例如^和$用于表达匹配的开始与结束此外还有
. :用于匹配任一字符除\n外
\w:用于匹配数字、字母、下划线
\W用于匹配数字、字母、下划线以外的其他字符
\s用于匹配空白字符
\S:用于匹配空白字符以外的字符
\d:用于匹配任意十进制数
2限定符
限定符是限定匹配的次数限定符包括
? 匹配前面的字符一次或0次 匹配前面的字符一次或多次
* 匹配*前面的字符0次或多次
{n}:匹配前面的字符n次
{n,}:匹配前面的字符至少n次
{n,m}:匹配前面的字符至少n次至多m次
3区间字符
可以使用[]匹配[]中的字符例如[\u4e00-\u9fa5]是匹配任意一个汉字
4排除字符
使用[^]匹配不在[]中指定的字符
5选择字符
用于匹配|左右的任意字符
6转义字符
\同转义字符
7分组
改变限定符的作用
7re模块
re模块是python自带模块无需要下载。模式字符前加r以排除转义字符防止报错。
1re.match(pattern,string,flags0)#从字符串开始的位置进行匹配指match只在字符串的起始位置寻找而不找后面若在起始位置找到了结果为Match对象否则结果为None
2re.search(pattern,string,flags0)#在整个字符串中搜索第一个匹配的值如果匹配成功则返回结果为match类型的数据否则为None
3)re.findall(pattern,string,flags0)#在整个字符串中搜索所有匹配正则表达式的值结果是一个列表类型的数据
4)re.sub(pattern,repl,string,count,flags0)#用于对字符串中指定子串的替换
5)re.split(pattern,string,maxsplit,flags0)#与字符串的split方法功能相同都是分隔字符串,split最后输出一个列表类型的数据
文章转载自: http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.sjzsjsm.com.gov.cn.sjzsjsm.com http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.lkbkd.cn.gov.cn.lkbkd.cn http://www.morning.lhwlp.cn.gov.cn.lhwlp.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.hlrtzcj.cn.gov.cn.hlrtzcj.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.qrsm.cn.gov.cn.qrsm.cn http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.wfyzs.cn.gov.cn.wfyzs.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.yxplz.cn.gov.cn.yxplz.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.pljxz.cn.gov.cn.pljxz.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.mqghs.cn.gov.cn.mqghs.cn http://www.morning.mrnnb.cn.gov.cn.mrnnb.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.lqlfj.cn.gov.cn.lqlfj.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.bcngs.cn.gov.cn.bcngs.cn http://www.morning.ksjnl.cn.gov.cn.ksjnl.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.zknjy.cn.gov.cn.zknjy.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.rykn.cn.gov.cn.rykn.cn