一:字符串以及正则表达式

二:字符串的常用操作

字符串是python中不可变的数据类型

#1:大小写转换
s1 = 'HelloWorld'
new_s1 = s1.lower()
print(new_s1)

new_s3 = new_s1.upper()
print(new_s3)

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

#3:
print(s1.count('o'))

#4:检索操作
print(s1.find('O'))  #o在字符串s1中首次出现的位置
print(s1.find('p'))  #-1,没有找到

#
print(s1.index('o'))
print(s1.index('p'))  #找不到,报错


#判断前缀和后缀
print(s1.startswith('H'))  #True
print(s1.startswith('p'))  #False

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

#1:替换
word = 'HelloWord'
new_word = word.replace('o','你好',1)
print(new_word)


#2:宽度范围居中
print(word.center(20))


#3:去掉字符串左右的空格
h = '  hello   word'
print(h.strip())
print(h.lstrip())  #去除字符串左侧的空格
print(h.rstrip())  #去除字符串右侧的空格

#4;去掉指定的字符(与字符顺序无关,包含即可)
g = 'dl_HelloWord'
print(g.strip('ld'))
print(g.lstrip('ld'))
print(g.rstrip('ld'))




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

连接各种数据类型

#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方法(0,1,2对应是format里面的顺序)
print('姓名:{0},年龄:{1},成绩:{2}'.format(name,age,score))
print('姓名:{2},年龄:{0},成绩:{1}'.format(age,score,name))


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

h = 'helloworld'
#字符串的显示宽度为20,左对齐,空白部分使用*号填充 (helloworld**********)
print('{0:*<20}'.format(h))
# 右对齐 (**********helloworld)
print('{0:*>20}'.format(h))
#居中对齐(*****helloworld*****)
print('{0:*^20}'.format(h))


#居中对齐(*****helloworld*****)
print(h.center(20,'*'))

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

#浮点数小数部分的精度(.2f表示2位)
print('{0:.2f}'.format(3.1415689))  #3.14

#字符串类型,表示是最大的显示长度
print('{0:.5}'.format('helloworld')) #hello


#整数类型 (0表示a有一个值就可以了)
a = 567
 #二进制:1000110111,十进制:567,八进制:1067,十六进制:237
print('二进制:{0:b},十进制:{0:d},八进制:{0:o},十六进制:{0:X}'.format(a))

#浮点数类型(科学计算法)
b = 3.1415926
# 3.141593(保留两位小数),3.14E+00(科学计数法),3.141593e+00(科学计数法),314.16% (百分号)
print('{0:2f},{0:.2E},{0:2e},{0:.2%}'.format(b))

字符串的编码和解码

str = '伟大的中国梦'

#一个中文占三个字节;默认是utf-8,因为utf-8中文占三个字节
#一:编码 str ---> bytes

#1:默认使用utf-8转换
scode = str.encode(errors='replace')
  #b'\xe4\xbc\x9f\xe5\xa4\xa7\xe7\x9a\x84\xe4\xb8\xad\xe5\x9b\xbd\xe6\xa2\xa6'
print(scode)
# print(str.encode(encoding ='utf-8',errors = 'stric/ignore/replace'))

#2:使用GBK转换(一个中文占两个字节)
scode_gbk = str.encode('gbk',errors='replace')
  #b'\xce\xb0\xb4\xf3\xb5\xc4\xd6\xd0\xb9\xfa\xc3\xce'
print(scode_gbk)

#3:编码中的出错问题(ignore= 忽略;replace = 用?提示; strict = 报错)
a = '✌耶'
#b'?\xd2\xae'
scode_a1 = a.encode('gbk',errors='replace')
print(scode_a1)

#b'\xd2\xae'
scode_a2 = a.encode('gbk',errors='ignore')
print(scode_a2)

#UnicodeEncodeError: 'gbk' codec can't encode character '\u270c' in position 0: illegal multibyte sequence
# scode_a3 = a.encode('gbk',errors='strict')
# print(scode_a3)

#二:解码过程bytes ---> str
  #伟大的中国梦
print(bytes.decode(scode,'utf-8'))



3.2: 数据的验证

1:str.isdigit()只能是十进制的数字

2:str.isnumeric()包含阿拉伯数字,罗马数字,一二三。。。

总:数据验证结果都是BOOLEAN类型

print('123'.isdigit())  #TRUE
print('一二三'.isdigit()) #FLASE
#0b1010是二进制的123
print('0b1010'.isdigit()) #false
print('III'.isdigit()) #false
print('-'*50)
#所有字符都是数字
print('123'.isnumeric()) #true
print('一二三'.isnumeric()) #true
print('0b1010'.isnumeric()) #false
print('III'.isnumeric()) #true
print('壹贰叁'.isnumeric()) #true
print('-'*50)

#所有字符都素字母(包含中文字符)
print('hello你好'.isalpha()) #true
print('hello你好123'.isalpha()) #false
print('hello你好II'.isalpha()) #false
print('hello你好一二三'.isalpha()) #true
print('hello你好壹贰叁'.isalpha()) #true
print('-'*50)

#所有字符都是数字和字母
print('hello你好'.isalnum()) #true
print('hello你好123'.isalnum()) #true
print('hello你好II'.isalnum()) #true
print('hello你好一二三'.isalnum()) #true
print('hello你好壹贰叁'.isalnum()) #true
print('-'*50)

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

print('HelloWorld'.isupper()) #false
print('helloworld'.isupper()) #true
print('hello你好'.isupper())  #true
print('-'*50)

#所有字符都是首字母大写
print('Hello'.istitle()) #true
print('HelloWorld'.istitle()) #false
print('HelloWolrd'.istitle()) #false
print('Hello WOld'.istitle()) #false
print('Hello World'.istitle()) #true
print('Hello wolrd'.istitle()) #false
print('-'*50)

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

3.3: 数据的处理(字符串拼接)

1:使用+

2:使用字符串的join()

3:直接拼接

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

#1:使用+直接拼接
a1 = 'hello'
a2 = 'world'
print(a1+a2)  #helloworld

#2:使用join()拼接
print(a1.join(a2))  #whelloohellorhellolhellod
print(''.join([a1+a2])) #空字符串拼接 ,放在列表中 ; helloworld

# *代替列表中的,
print('*'.join(['hello','world','python','java'])) #hello*world*python*java
print('你好'.join(['hello','world','python','java'])) #hello你好world你好python你好java

#3:直接拼接
print('hello''world') #helloworld

#4:使用格式化字符串拼接
print('%s%s' %(a1,a2))
print(f'{a1}{a2}')
print('{0}{1}'.format(a1,a2))
a = 'helloworldaaduiollffee'
#(1)字符串拼接以及not in
new_s = ''
for item in a:
    if item not in new_s:
        new_s += item
print(new_s)

#(2)使用索引+not in
new_s2 = ''
for i in range(len(a)):
    if a[i] not in new_s:
        new_s += a[i]

print(new_s2)

#(3)通过集合去重 + 列表排序
new_s3 = set(a)
lst = list(new_s3)
lst.sort(key=a.index)
print(lst)  # ['l', 'w', 'e', 'o', 'i', 'd', 'h', 'f', 'a', 'r', 'u']

#拼接
print(''.join(lst))  #helowrdauif

3.4: 正则表达式

 

1:re模块

更多推荐