bb
1实用字符串索引与切片
1.1字符串索引
索引用来表示字符串中字符的所在位置,基于位置,可以快速找到其对应的字符
如果一个字符串中有n个字符,那么索引的取值范围是0~n-1
结构:<字符串or字符串变量>[索引]
举例:<s>[0]
code:
s="how are you doing?"
print(s[0])
输出结果:h
s="how are you doing?"
print(s[2])
输出结果:w
逆序索引
s="how are you doing?"
print(s[-2])
result:g
1.2字符串的切片
使用切片可以获取字符串指定索引区间的字符
结构:<字符串or字符串变量>[开始索引:结束索引:步长]
步长在某些情况可以不写
举例:<s>[0:8]
code:
s="how are you doing?"
print(s[1:6])
result:ow ar
注意到没有包含e,原因如下:
s[1:6]表示s大于等于1,小于6。不包含结束索引的字符
逆序切片情况
s="how are you doing?"
print(s[:-2])
result:how are you doin
注意不包含倒数第二个字符,仍然是开区间
s="how are you doing?"
print(s[1:])
result:ow are doing?
s="how are you doing?"
print(s[:5])
result:how a
s="how are you doing?"
print(s[:])
s[:]表示取全部
1.3步长
不写步长即默认步长为1
code:
s="how are you doing?"
print(s[0:11:2])
result:hwaeyu
步长为2代表每两个字符取第一个
逆序排列所需代码
s="how are you doing?"
print(s[::-1])
result:?gniod uoy era woh
2 for循环
for循环:遍历循环
while循环:无线循环
可迭代对象:字符串、列表、元组、字典等可以用来遍历循环的数据
结构:<循环变量>in<可迭代对象>
<循环体>
例子:
s="how are you doing?"
for i in s:
print(i)
实质:将字符串里的每一个字符进行遍历
结果:
3字符串常用函数
函数 | 描述 |
len(x) | 返回字符串x的长度 也可返回其他组合数据类型的元素个数 |
str(x) | 返回任意类型x所对应的字符串类型 |
chr(x) | 返回Unicode编码x对应的单个字符 |
ord(x) | 返回单字符x表示的Unicode编码 |
hex(x) | 返回整数x对应十六进制数的小写形式字符串 |
oct(x) | 返回整数x对应八进制数的小写形式字符串 |
Unicode编码是什么?
概念辨析https://baike.baidu.com/item/%E7%BB%9F%E4%B8%80%E7%A0%81/2985798?fr=ge_ala
使用举例:
4字符串常用处理方法
(表格)
方法 | 描述 |
str.lower() | 返回字符串str的副本,全部字符小写 |
str.upper() | 返回字符串str 的副本,全部字符大写 |
str.split(sep=None) | 返回一个列表,由str根据sep被分割的部分构成,省略sep默认以空格分隔 |
str.count(sub) | 返回sub字串出现的次数 |
str.replaced(old,new) | 返回字符串str的副本,所有old字串被替换为new |
str.center(width,fillchar) | 字符串居中函数,fillchar参数可选 |
str.strip() | 从字符串str中去掉在其左侧和右侧chars中列出的字符 |
str.join(iter) | 将iter变量的每一个元素后增加一个str字符串 |
split函数
应用场景:提取并放在列表里
s='asd-f-oy-ed-gv-kl-23'
a=s.split('-')
print(a)
运行结果
['asd', 'f', 'oy', 'ed', 'gv', 'kl', '23']
列表是什么见后文
count函数
应用场景:某一个字符串在当前字符串中出现了多少次
s='123dkcuidj123hssk123sisdhsd123sssk123'
a=s.count('123')
print(a)
运行结果
5
replace函数
s1='HelloWorld'
s2=s1.replace('o','O')
print(s2)
HellOWOrld
进程已结束,退出代码为 0
center函数
应用场景:width是最后字符串的总长度,fillchar是填充进去的字符。
可以用于美化网页
s='hell0'
print(s.center(11,'#'))
###hell0###
进程已结束,退出代码为 0
如果fillchar位置为空,则默认用空格进行填充
s='hell0'
print(s.center(11))
hell0
进程已结束,退出代码为 0
如果width长度小于字符串,则返回字符串本身
s='hell0'
print(s.center(2,'#'))
hell0
进程已结束,退出代码为 0
strip函数
strip函数括号内不填,表示自动删除左右两边的空格
s1=' python '
print(s1.strip())
python
进程已结束,退出代码为 0
strip函数内填写指定内容,即删除字符串两边的指定内容(一定是最左和最右)
s1='==python=='
print(s1.strip('=='))
python
进程已结束,退出代码为 0
不是最左和最右时,将无法删除
s1='cc==python==cc'
print(s1.strip('=='))
cc==python==cc
进程已结束,退出代码为 0
join函数
将manwhatcanisay作为iter变量,其后每一个元素增加一个“,”
print(','.join('manwhatcanisay'))
m,a,n,w,h,a,t,c,a,n,i,s,a,y
进程已结束,退出代码为 0
5.其他函数
capitalize函数
应用场景:将字符串转换为标题的字符串(首字母大写的字符串)
s='i love you'
print(s.capitalize())
I love you
进程已结束,退出代码为 0
index函数
格式:index(sub,begin,end)
应用场景:返回sub在当前字符串中第一次出现的位置,如果没找到,报错
s='What to Know About the Controversy Over Trump’s Visit to Arlington National Cemetery'
print(s.index('g'))
62
进程已结束,退出代码为 0
s='What to Know About the Controversy Over Trump’s Visit to Arlington National Cemetery'
print(s.index('g',16,27))
Traceback (most recent call last):
File "D:\代码\pycharm项目学习\pythonProject\jsjej1.py", line 2, in <module>
print(s.index('g',16,27))
^^^^^^^^^^^^^^^^^^
ValueError: substring not found
进程已结束,退出代码为 1
find函数
格式:find(sub,begin,end)
应用场景:返回sub在当前字符串中第一次出现的位置,如果没找到,返回-1
s='What to Know About the Controversy Over Trump’s Visit to Arlington National Cemetery'
print(s.find('g',16,27))
-1