字符串
字符串是python中的不可变数据类型
字符串的常用操作
- replace第三个参数为替换次数,默认为全部替换
- strip()括号里不写,默认就是左右全部空格都去掉,但是如果括号里写了字符串,那就是是只去掉指定字符串
格式化字符串的三种方法
name='马冬梅'
age=18
score=98.5
# 1.使用占位符进行格式化
print('姓名:%s,年龄:%d,成绩:%.1f' %(name,age,score))
#2.f-string
print(f'姓名:{name},年龄:{age},成绩{score}')
#3.使用字符串format方法
print('姓名:{0},年龄:{1},成绩:{2}'.format(name,age,score))
格式化字符串 format的详细格式
s='helloworld'
print('{0:*<20}'.format(s))#字符串的显示宽度为20,左对齐,空白部分使用*填充
#千位分隔符(只适用于整数和浮点数)
print('{0:,}'.format(987654321))
#浮点数小数的精度
print('{0:.2f}'.format(3.1415926))
#最大显示长度(与上面精度相比,没有写f)
print('{0:.5}'.format('helloword'))
#整数类型
a=425
print('二进制:{0:b},十进制{0:d},八进制{0:o},十六进制{0:X}'.format(a))
#浮点数类型
b=3.1415926
print('{0:.2f},{0:.2E},{0:.2e},{0:.2%}'.format(b))
#3.14,3.14E+00,3.14e+00,314.16
字符串的编码与解码
- 默认是utf-8,中文占3个字节;gbk中文占2个字节
数据的验证
数据的处理
字符串的拼接
s1='hello'
s2='world'
# 1.使用加号+拼接
print(s1+s2)
# 2.使用字符串的join()方法
#指定空字符串进行拼接
print(''.join([s1,s2]))#把s1和s2放到列表,那么它就会对列表里的元素进行拼接
# 3.直接拼接
print('hello' 'world')
# 4.使用格式化字符串进行拼接
print('%s%s' % (s1,s2))
print(f'{s1}{s2}')
print('{0}{1}'.format(s1,s2))
字符串的去重操作
s='hellohelloworldhelloword'
#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_s)#转成列表类型
lst.sort(key=s.index)
print(''.join(lst))
正则表达式(特殊的字符序列)
元字符
限定符
其他字符
re模块
match函数的使用
import re#导入
pattern='\d.\d+'#+限定符.\d 0-9数字出现次或多次
s='I study Python 3.12 everyday'#待匹配字符串
match=re.match(pattern,s,re.I)
print(match)#结果是None
s2='3.12Python I study every day'
match2=re.match(pattern,s2)
print(match2)#<re.Match object; span=(0, 4), match='3.12'>
print('匹配的起始位置:',match2.start()) #0
print('匹配的结束位置:',match2.end()) #4
print('匹配区间的位置元素:',match2.span())#(0,4)
print('待匹配的字符串:',match2.string)#3.12 I study Python everyday
print('匹配的数据:',match2.group())#3.12
search函数的使用
import re
pattern='\d.\d+'
s='I study Python 3.12 everyday'
match=re.search(pattern,s)
print(match)
findall函数的使用
sub函数的使用
import re
pattern='黑客|破解|反爬'
s='我想学习Python,想破解一些VIP视频,Python可以实现无底线反爬吗?'
new_s=re.sub(pattern "XXX' ,s)
print(new_s)
split函数的使用
import re
s2='https://www.baidu.com/?tn=62095104_17_oem_dg'
pattern2='[?|_]'
lst=re.split(pattern2,s2)
print(lst)
实战
实战一
# 1. 要求根据车牌号,通过切片操作找到其归属地
lst=['京A1111','津B6666','冀C8888']
for item in lst:
area=item[0:1]
print(item,'归属于地',area)
实战二
# 2.忽略大小写,查找字符出现次数
word=input('请输入你要统计的字符:')
s='HelloPython,HelloJave,hellophp'
print('{0}在{1}一共出现了{2}次'.format(word,s,s.upper().count(word)))
实战三
# 3.格式化输出商品的名称和单价
lst=[
['01','电风扇','美的','500'],
['02', '洗衣机','海尔', '1000'],
['03','微波炉','老板','400']
]
print('编号\t\t名称\t\t品牌\t\t单价')
for item in lst:
for i in item:
print(i,end='\t\t')
print()
#格式化
for item in lst:
item[0]='0000'+item[0]
item[3]='${0:.2f}'.format(eval(item[3]))
print('编号\t\t\t名称\t\t品牌\t\t单价')
for item in lst:
for i in item:
print(i,end='\t\t')
print()
异常处理
Bug 它指的是检测并排除计算机程序/机器中的故障
异常处理机制的语法结构
raise关键字
try:
gender=input('请输入你的性别:')
if gender!='男' and gender!='女':
raise Exception('性别只能是男或者女')
else:
print('你的性别是:',gender)
except Exception as e:
print(e)
Python中常见的异常类型
PyCharm的程序调试
PyCharm进行代码调试的操作步骤:
1.设置断点——>2.进入调试视图(三种方式)——>3.开始调试
断点一般设置在循环处
实战
实战一
#输入成绩,如果不正确手动抛出异常
# 手动抛出异常用raise
try:
score=eval(input('请输入你的成绩:'))
if 0<score<100:
print('分数为:',score)
else:
raise Exception('分数不正确')
except Exception as e:
print(e)
实战二
# 勾股判断三边是否构成三角形(不能的话抛出异常)
try:
a=int(input('请输入第一条边长:'))
b = int(input('请输入第二条边长:'))
c = int(input('请输入第三条边长:'))
if a+b>c and a+c>b and b+c>a:
print(f'三边长为:{a},{b},{c}')
else:
raise Exception(f'{a},{b},{c},不可以构成三角形')
except Exception as e:
print(e)