字符串及正则表达式

embedded/2024/10/20 13:16:01/

目录

字符串

字符串常用方法:

格格式化字符串的三种方式:

格式化字符串的详细格式:

字符串的编码:

字符串的解码:

数据的验证:

字符串拼接的几种方式:

字符串去重:

正则表达式

元字符:

限定符:

正则表达式:

re模块:

match的用法:

search的使用:

findal的使用:

sud的使用:

split的使用:


字符串

字符串常用方法:

格格式化字符串的三种方式:

1.占位符:%s :字符串格式 %d :十进制整数格式 %f :浮点数格式 

2.f-string:Python3.6引入的格式化字符串的方式,比{}标明被替换的字符

3.str.format()方法:模板字符串.format(逗号分隔的参数)

formatted_string = "{1} is from {0}.".format("New York", "Bob")
print(formatted_string)

格式化字符串的详细格式:

字符串的编码:

将str类型转换成bytes类型,需要使用到字符串的encode()方法

str.encode(encoding=‘utf-8’,errors=‘strict/ignore/replace’)

字符串的解码:

将bytes类型转换成str类型,需要使用到bytes类型的decode()方法

bytes.decode(encoding=‘utf-8’,errors=‘strict/ignore/replace’)

数据的验证:

字符串拼接的几种方式:

1、使用加号进行拼接

s1='hello'
s2='world'
print(s1+s2)

2、使用字符串的join()进行拼接

s1='hello'
s2='world'
print(''.join([s1,s2]))
print('你好'.join(['hello','world','python','css']))

3、直接拼接

print('hello''world')

4、用格式化字符串进行拼接

print('%s%s'%(s1,s2))
print(f'{s1}{s2}')
print('{0}{1}'.format(s1,s2))

字符串去重:

1.字符串拼接及not in

s='helloworldhelloworldadfdfdeoodllffe'
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))​

正则表达式

元字符:

限定符:

正则表达式:

re模块:

match的用法:

import re
pattern='\d\.\d+'
s='I study python 3.11 every day'
match=re.match(pattern,s,re.I)
print(match)
s2='3.11Python I study every day'
match2=re.match(pattern,s2)
print(match2)
print('匹配值的起始位置:',match2.start())
print('匹配值的结束位置:',match2.end())
print('匹配区间的位置元素:',match2.span())
print('待匹配的字符串:',match2.string)
print('匹配的数据:',match2.group())

search的使用:

import re
pattern='\d\.\d+'
s1=('I study Python3.11 every day Python2.7 I love you')
match=re.search(pattern,s)
print(match)

findal的使用:

​
import re
pattern='\d\.\d+'
s='I study Python3.11 every day Python2.7 I love you'
s2='4.10 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)
print(lst2)
print(lst3)​

sud的使用:

import re
pattern='黑客|破解|反爬'
s='我想学习Python,想破解一些VIP视频,Python可以实现无底线反爬吗?'
new_s=re.sub(pattern,repl:'xxx',s)
print(new_s)

split的使用:

s2='https://www.baidu.com/s?wd=ysj&rsv_spt=1'
pattern2='[?|&]'
lst=re.split(pattern2,s2)
print(lst)


http://www.ppmy.cn/embedded/129001.html

相关文章

成语积累学习

识文断字:有一点文化知识 雨后春笋:春雨过后快速生长的竹笋;比喻大量涌现的新生事物 味同嚼蜡:如同咀嚼白蜡一样,毫无味道。形容文章或言辞枯燥乏味。 差强人意:大体上让人满意 八面玲珑:处…

STC15W408AS直流有刷电机定时器0PWM 脉宽输出

/***2024 测试直流电机正反转past***/ /***2024 9 27板2024 10 20*L1000 CODE4422**/ #include <REG52.H> #include <intrins.h> #include <string.h> #include "s…

SpringCloudAlibaba升级手册

目录 1. 版本对照 版本现状 SpringCloud与AlibabaCloud对应版本 Springboot与Elasticsearch版本对应 2. openfeign问题 问题 解决方案 3. Feign请求问题 问题 解决方法 4. Sentinel循环依赖 问题 解决方案 5. bootstrap配置文件不生效 问题 解决方案 6. Nacos的…

软考(网工)——网络安全

文章目录 &#x1f550;网络安全基础1️⃣网络安全威胁类型2️⃣网络攻击类型 &#x1f551;现代加密技术1️⃣私钥密码/对称密码体制2️⃣对称加密算法总结3️⃣公钥密码/非对称密码4️⃣混合密码5️⃣国产加密算法 - SM 系列6️⃣认证7️⃣基于公钥的认证 &#x1f552;Hash …

AcWing1027

题目重述&#xff1a; 题目的核心是找到一条路径的最大权值总和&#xff0c;但路径要从起点 (1, 1) 走到终点 (n, n)。由于两条路径分别经过不同的格子&#xff0c;我们可以巧妙地将问题简化为两次同时出发的路径问题。这种映射的设计让我们能够更方便地处理两条路径重叠在同一…

探索 Python Web 开发:从框架到爬虫

Python 是 Web 开发中广泛使用的编程语言&#xff0c;因其简单、灵活和强大的生态系统&#xff0c;适合构建各种类型的 Web 应用和 API。在本篇博客中&#xff0c;我们将讨论 Web 开发的几个重要主题&#xff0c;包括 Flask 和 Django 框架、API 开发、HTTP 请求处理以及网页爬…

Qt贪吃蛇-游戏逻辑设计(4)

目录 游戏房间 实现游戏开始和游戏暂停 设置移动按钮 退出游戏 绘制分数 绘制游戏失败效果 控制蛇的移动速度 添加吃食物的音效 选择大厅 历史记录绑定信号槽 往文件中写入分数 承接上文 游戏房间 实现游戏开始和游戏暂停 setLoops 参数是循环的次数&#xff0c…

如何快速学会盲打

今天就来给大家分享一下如何快速学会盲打 盲打的基本方法和步骤 手指放置&#xff1a;将双手放在键盘上&#xff0c;左手食指放在F键上&#xff0c;右手食指放在J键上&#xff0c;其他手指分别放在相邻的键位上。熟悉键盘布局&#xff1a;学习26个字母的位置&#xff0c;以及…