Python学习 第6章-字符串及正则表达式

本章内容:

> 字符串的常用方法:

  • 大小写转换方法:str.lower()、str.upper()
  • 字符串分隔方法:str.split()
    检索的方法:str.count()、str.find()、str.index()
  • 字符串判断的方法:str.startswith()、str.endswith()
  • 字符串替换的方法:str.replace()
  • 字符串显示方式的方法:str.center()
    字符串拼接方法:strjoin()
  • 去除字符串前后字符的方法:str.strip()、str.lstrip()、str.rstrip()

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

  • 使用占位符进行格式化字符串
  • f-string格式化字符串
  • 使用字符串的format方法进行格式化字符串

> 字符串编码的方法:str.encode()

> 字符串解码的方法:bytes.decode()

> 数据验证的方法:str.isdigit()、str.isnumeric()、str.isalpha()、str.isalnum()、str.islower()、

strisupper(), stristitle(). str.isspace()

> 数据的处理:字符串的拼接与去重

> 内置模块re中的常用的函数:re.match()、re.search()、re.findall()、re.sub()、re.split()

本章示例:

示例6-1字符串的相关处理方法1

s1='HelloWorld'
new_s2=s1.lower()
print(s1,new_s2)

new_s3=s1.upper()
print(new_s3)

#字符串的分割
e_mail='abc@126.com'
lst=e_mail.split('@')
print('邮箱名:',lst[0],'邮箱服务器域名:',lst[1])


print(s1.count('o'))#o在字符串s1中出现的次数

print(s1.find('o'))#o在字符串s1中首次出现的位置
print(s1.find('p'))

print(s1.index('o'))
#print(s1.index('p')) #ValueError: substring not found


#判断前缀和后缀
print(s1.startswith('H'))
print(s1.startswith('P'))

print('demo.py'.endswith('.py'))
print('text.txt'.endswith('.txt'))

示例6-2字符串的相关处理方法2

s='HelloWorld'
new_s=s.replace('o','你好',1)
print(new_s)

#字符串在指定的宽度范围内居中
print(s.center(20))
print(s.center(20,'*'))

#去掉字符串左右的空格
s='    Hello    World    '
print(s.strip())#去掉字符串左右两侧空格
print(s.lstrip())#去掉字符串左侧空格
print(s.rstrip())#去掉字符串右侧空格

#去掉指定的字符
s3='dl-HelloWorld'
print(s3.strip('ld'))
print(s3.lstrip('ld'))
print(s3.rstrip('ld'))

示例6-3格式化字符串

#(1)使用占位符进行格式化
name='马冬梅'
age=18
score=98.5
print('姓名:%s,年龄:%d,成绩:%f' %(name,age,score))
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))
print('姓名:{2},年龄:{0},成绩:{1}'.format(age,score,name))

示例6-4format的格式控制

s='helloworld'
print('{0:*<20}'.format(s))
print('{0:*>20}'.format(s))
print('{0:*^20}'.format(s))


print(s.center(20,'*'))


#千位分隔符(只适用于整数和浮点数
print('{0:,}'.format(987654321))
print('{0:,}'.format(987654321.7865))


#浮点数小数部分的精度
print('{0:.2f}'.format(3.1415926))
print('{0:.5}'.format('helloworld'))

#整数类型
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))

示例6-5字符串的编码与解码

s='伟大的中国梦'
scode=s.encode(errors='replace')#默认是utf-8
print(scode)

scode_gbk=s.encode('gbk',errors='replace')
print(scode_gbk)


s2='耶😂'
scode_error=s2.encode('gbk',errors='ignore')
print(scode_error)

# scode=s2.encode('gbk',errors='strict')
# print(scode)
# UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f602' in position 1: illegal multibyte sequence
# encoding with 'gbk' codec failed

scode_error=s2.encode('gbk',errors='replace')
print(scode_error)


print(bytes.decode(scode_gbk,'gbk'))
print(bytes.decode(scode,'utf-8'))

示例6-6数据的验证

#所有字符都是十进制阿拉伯数字
print('123'.isdigit())
print('一二三'.isdigit())
print('0b1010'.isdigit())
print('Ⅲ'.isdigit())
print('-'*40)

#所有字符都是数字
print('123'.isnumeric())
print('一二三'.isnumeric())
print('0b1010'.isnumeric())
print('Ⅲ'.isnumeric())
print('壹贰叁'.isnumeric())
print('-'*40)
#所有字符都是字母
print('hello你好'.isalpha())
print('hello你好123'.isalpha())
print('hello你好一二三'.isalpha())
print('hello你好ⅢⅢⅢ'.isalpha())
print('hello你好壹贰叁'.isalpha())
print('-'*40)

#所有字符都是数字或字母
print('hello你好'.isalnum())
print('hello你好123'.isalnum())
print('hello你好一二三'.isalnum())
print('hello你好ⅢⅢⅢ'.isalnum())
print('hello你好壹贰叁'.isalnum())


#判断字符的大小写
print('HelloWorld'.islower())
print('helloworld'.islower())
print('hello你好'.islower())
print('-'*40)

print('HelloWorld'.isupper())
print('helloworld'.isupper())
print('hello你好'.isupper())
print('-'*40)

#所有字符首字母大写
print('Hello'.istitle())
print('HelloWorld'.istitle())
print('Helloworld'.istitle())
print('Hello World'.istitle())
print('Hello world'.istitle())
print('-'*40)

#判断是否是空白字符
print('\t'.isspace())
print(' '.isspace())
print('\n'.isspace())

示例6-7字符串的拼接操作

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

print(''.join([s1,s2]))

print('*'.join(['hello','world','python','java','php']))
print('你好'.join(['hello','world','python','java','php']))


print('hello''world')


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

示例6-8字符串的去重操作

s='helloworldhelloworldadfdafdfafbdfae'
#(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))

示例6-9match函数的使用

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())

示例6-10search函数的使用

import re
pattern='\\d\\.\\d+'
s='I study python 3.11 every day python 2.7 I love you'
match=re.search(pattern,s)

s2='4.10 python I study every day'
s3='I study python every day'
match2=re.search(pattern,s2)
match3=re.search(pattern,s3)
print(match)
print(match2)
print(match3)

print(match.group())
print(match2.group())

示例6-11findall函数的使用

import re
pattern='\\d\\.\\d+'
s='I study python 3.11 every day python 2.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)

示例6-12sub函数与split函数的使用

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

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

注:本文中所有内容及示例均出自@Python_子木的视频

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值