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

方法名 描述说明
str.lower() 将str字符串全部转化成小写字母,结果为一个新的字符串
str.upper() 将str字符串全部转换成大写字母,结果为一个新的字符串
str.split(sep=None) 把str按照指定的分隔符sep进行分隔,结果为列表类型
str.count(sub) 结果为sub这个字符串在str中出现的次数
str.find(sub) 查询sub这个字符串在str中是否存在,如果不存在结果为-1,如果存在,结果为sub首次出现的位置
str.index(sub) 功能与find()相同,区别在于要查询的子串sub不存在时,程序报错
str.startswith(s) 查询字符串str是否以子串s开头
str.endswith(s) 查询字符串str是否以子串s结尾
str.replace(old,news) 使用news替换字符串s中所有有的old在字符串,结果是一个新的字符串
str.center(width,fillchar) 字符串在指定的宽度范围内居中,可以使用fillchar进行填充
str.join(iter) 在iter中的每个元素的后面都增加一个新的字符串str
str.strip(chars) 从字符串中去掉左侧和右侧chars中列出的字符串
str.lstrip(chars) 从字符串中去掉左侧chars中列出的字符串
str.rstrip(chars) 从字符串中去掉右侧chars中列出的字符串

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

s='helloWorld'
#全部转换成小写
new_s=s.lower()
print(new_s)
#全部转换成大写
new_s=s.upper()
print(new_s)
#分隔字符串
l=s.split('l')
print(l)
#查询目标字符在字符串出现的次数
count=s.count('l')
print(count)
#查询目标字符在字符串中首次出现的位置,如果不存在,结果为-1
idx=s.find('l')
print(idx)
idx=s.find('p')
print(idx)
#index()函数与find()函数功能类似,如果不存在,则报错
idx=s.index('l')
print(idx)
# idx=s.index('p')
# print(idx)
#判断是否某个子串开头
print('以hello开始的吗?',s.startswith('hello'))
#判断是否以某个子串结束
print('以hello结束的吗?',s.endswith('hello'))

#替换字符串 可以指定替换的次数
new_s=s.replace('hello','python')
print(new_s)
new_s=s.replace('l','你好',2)
print(new_s)

#在指定的范围内居中显示,其他空白的用fillchar填充
new_s=s.center(20,'*')
print(new_s)
print(s.center(20))

#join()函数每个字符增加字符串s
new_s=s.join('abc')
print(new_s)  #ahelloWorldbhelloWorldc

s='   hello   world   '
#去掉左右两侧的空格
print(s.strip())
#去掉左侧的空格
print(s.lstrip())
#去掉右侧的空格
print(s.rstrip())

#去掉指定的字符
s='**helloworld**'
#去掉左右两侧的*
print(s.strip('*'))
#去掉左侧的*
print(s.lstrip('*'))
#去掉右侧的*
print(s.rstrip('*'))

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

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

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

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

name='赵子龙'
age=23
score=90.5
#占位符
print('姓名:%s,年龄:%d,分数:%.2f' %(name,age,score))
#f-string
print(f'姓名:{name},年龄:{age},分数:{score}')

#format
print('姓名:{0},年龄:{1},分数:{2}'.format(name,age,score))
print('姓名:{2},年龄:{0},分数:{1}'.format(age,score,name))

格式化字符串的详细格式

填充 对齐方式 宽度 , 精度 类型
引导符号 用于填充单个字符

< 左对齐

> 右对齐

^ 居中对齐

字符串的输出宽度 数字的千位分隔符 浮点数小数部分的精度或字符串的最大输出长度

整数类型:b\d\o\x\X

浮点数类型:e\E\f\%

 

 

 

 

 

 

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

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

# 千分位符
print('{0:,}'.format(98765432))
print('{0:,}'.format(9876.4432))

# 浮点数进度
print('{0:.2f}'.format(3.1415926))
#字符串类型,表示最大的显示长度
print('{0:.5}'.format('helloworld')) #只显示hello
#整数类型
a=200
print('二进制:{0:b},十进制:{0:d},八进制:{0:o},十六进制:{0:x},十六进制:{0:X}'.format(a))
#浮点数类型
b=3.1415928
print('{0:.2f},{0:.2E},{0:.2e},{0:.2%}'.format(b))

字符串的编码和解码

字符串的编码:将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')
s='北京欢迎你'
scode=s.encode(errors='replace')  #默认编码格式是utf-8 英文是占一个字节,中文占三个字节
print(scode)

scode_gbk=s.encode(encoding='gbk',errors='strict') #gbk 英文占一个字节,中文占三个字节
print(scode_gbk)

print(bytes.decode(scode,'utf-8',errors='replace'))
print(bytes.decode(scode_gbk,'gbk',errors='strict'))

数据的验证

数据的验证是指程序对用户输入的数据进行“合法”性验证。

方法名 描述说明
str.isdigit() 所有字符都是数字(阿拉伯数字)
str.isnumeric() 所有字符都是数字
str.isalpha() 所有字符都是字母(包含中文字符)
str.isalnum() 所有字符都是数字或字母(包含中文字符)
str.islower() 所有字符都是小写
str.isupper() 所有字符都是大写
str.istitle() 所有字符都是首字母大写
str.isspace() 所有字符都是空白字符(\n,\t等)

 

 

 

 

 

 

 

 

 

#isdigit()判断是否是阿拉伯数字
print('123'.isdigit()) #True
print('一二三'.isdigit()) #False
print('0b1010'.isdigit()) #False
print(''.isdigit()) #False

print('*'*50)
# isnumeric()函数判断是数字
print('123'.isnumeric()) #True
print('一二三'.isnumeric()) #True
print('0b1010'.isnumeric()) #False
print(''.isnumeric()) #True
print('壹贰叁'.isnumeric()) #True

print('*'*50)
# isalpha()判断是否是字母(包含中文)
print('hello你好'.isalpha()) #True
print('hello你好123'.isalpha()) #False
print('hello你好一二三'.isalpha()) #True
print('hello你好Ⅲ'.isalpha()) #False

print('*'*50)
#isalnum()判断是否是数字和字母(包含中文)
print('hello你好'.isalnum()) #True
print('hello你好123'.isalnum()) #True
print('hello你好一二三'.isalnum()) #True
print('hello你好Ⅲ'.isalnum()) #True

print('*'*50)
#islower()判断是否是字母(包含中文)小写
print('helloWorld'.islower()) #False
print('helloworld'.islower()) #True
print('helloworld你好'.islower()) #True

print('*'*50)
#isupper()判断是否是字母(包含中文)大写
print('HELLOWORLD'.isupper()) #True
print('helloworld'.isupper()) #False
print('HELLOWORLD你好'.isupper()) #True

print('*'*50)
#istitle()判断所有字母是否首字母大写
print('HelloWorld'.istitle()) #Fasle
print('Hello'.istitle()) #True
print('Helloworld'.istitle()) #True
print('Hello World'.istitle()) #True
print('Hello world'.istitle()) #False

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

数据的处理

字符串拼接的几种方式:

1,使用str.join()方法进行拼接字符串

2,直接拼接

3,使用格式化字符串进行拼接

s1='hello'
s2='world'
print('*'*50)
#1)直接使用+拼接
print(s1+s2)

print('*'*50)
#2)使用字符串的join()方法
print(''.join([s1,s2])) #使用空字符串进行拼接
print('*'.join(['hello','wolrd','python']))

print('*'*50)
#3)直接拼接
print('hello'"world")

print('*'*50)
#4)使用格式化字符串拼接
print('%s%s' %(s1,s2))
print(f'{s1}{s2}')
print('{0}{1}'.format(s1,s2))

字符串的去重操作

s='hellohelloworldhello123'

# 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_s=''
for i in range(0,len(s)):
    if s[i] not in new_s:
        new_s+=s[i]
print(new_s)

# 3)通过集合+列表排序
new_s=set(s)
l=list(new_s)
l.sort(key=s.index)  #按照原来的排序恢复过来
print(''.join(l))

 

 posted on 2023-12-16 18:50  会飞的金鱼  阅读(28)  评论(0)    收藏  举报