python字符串类型
目录
字符串
字符串取值
字符串下标
字符串切片
字符串查找
find
index
count
字符串修改
replace
split
字符串转换
capitalize
lower
upper
title
字符串判断
islower
isupper
isdigit
startswith
endswith
字符串删除
lstrip
rstrip
strip
字符串
用单引号或者双引号包含的字符就是字符串。
str1 = 'hello world'
str2 = "hello music"
字符串取值
字符串下标
我们可以通过字符串的下标获取到字符串的值。
下标从0开始,并且python中的下标支持负数。
正数下标:
str1 = "hello music"
print(str1[0]) #h
print(str1[1]) #e
print(str1[2]) #l
print(str1[3]) #l
print(str1[4]) #o
print(str1[5]) #
print(str1[6]) #m
print(str1[7]) #u
print(str1[8]) #s
print(str1[9]) #i
print(str1[10]) #c
负数下标:
负数可以理解为从右往左取字符。如果我们要获取字符串的最后一个字符,就可以使用负数下标。
str1 = "hello music"
print(str1[-1]) #c
print(str1[-2]) #i
print(str1[-3]) #s
print(str1[-4]) #u
print(str1[-5]) #m
print(str1[-6]) #
print(str1[-7]) #o
print(str1[-8]) #l
print(str1[-9]) #l
print(str1[-10]) #e
print(str1[-11]) #h
如果下标超过范围会报错。
str1 = "hello music"
print(str1[20]) #h
下标小结:
1.用正数表示索引值,从左向右定位,从0开始计数,如0、1、2。
2.用负数表示索引值,从右向左定位,从-1开始计数,如-1、-2、-3。
3.下标如果超出范围,会报错。
字符串切片
我们取出一个字符串的子串,如果通过下标的方式,然后用+拼接起来,比如下面代码中我们要取出music,只能下标一个个获取然后拼接,这样其实很麻烦,我们可以通过切片的方式获取。
str1 = "hello music"
print(str1[6] + str1[7] + str1[8] + str1[9] + str1[10])
切片语法:[起始索引:结束索引:步长] (范围:左闭右开,顾头不顾尾)
m的下标是6,因此起始位置就是6,c的下标是10,结束索引是不包含自己的,因此是11,步长是1,也就是一个个取,如果步长是2,那就表示依次跳2步,m取完跳到s在跳到c。如果我们没有限制就一个:,那就是取到所有。如果步长是负数,表示从右往左取。第四个语句,起始位置-1也就是从最后一个位置开始取。
str1 = "hello music"
print(str1[6:11:1]) # 步长为1, music
print(str1[6:11:2]) # msc
print(str1[:]) # hello music 没有限制取到所有
print(str1[-1:3:-1])# 倒序输出 cisum o
print(str1[0:20:1]) # 如果超出范围不会报错,能取多少取多少
print(str1[2:5:-1]) # 2-5是从左往右的,然后-1又是从右往左的,冲突了,因此啥都不会输出
切片小结:
1.指定区间属于左闭右开。
2.步长可以控制查找方向,如果为正,从左往右,如果为负,从右往左。
3.切片时,索引超出范围不会报错,查找范围截止到最后一位。
字符串查找
find
查找字符或字符串是否包含在字符串中,存在返回它第一次出现的下标,不存在返回-1。
str1 = "hello music"
print(str1.find('m')) # 6
print(str1.find('music')) # 6
print(str1.find('x')) # -1
# find(str, 开始下标,结束下标)
print(str1.find('o', 1, 5)) # 4 从[1,5)区间内查找字符o
index
和find功能一样,区别在于如果不存在会报错。
str1 = "hello music"
print(str1.index('m')) # 6
print(str1.index('music')) # 6
print(str1.index('x')) # 找不到会报错
# index(str, 开始下标,结束下标)
print(str1.index('o', 1, 5)) # 4 从[1,5)区间内查找字符o
count
统计一个字符出现的次数,返回出现的次数。
str1 = "hello music"
print(str1.count('l')) # 2
print(str1.count('music')) # 1
# count(str, 起始位置,结束位置)
print(str1.count('l', 5)) # 0
print(str1.count('l', 2, 3)) # 1
字符串修改
replace
字符串替换,会返回一个替换后的新字符串,但不会修改原字符串。
语法:字符串序列.replace(旧子串,新子串,替换次数)
str1 = "hello music"
# l全都换成o,但是原字符串不会改变,会返回一个替换后的字符串
print(str1.replace('l', 'o')) # heooo music
print(str1.replace('l', 'o', 1)) # heolo music
print(str1) # hello music
split
按照某个分隔符将原字符串分割,会返回一个列表。
语法:mystr.split(str,切的次数) 用 str来切原字符串。
str1 = "hello music"
print(str1.capitalize()) #Hello music
print(str1) #hello music
字符串转换
capitalize
第一个字符转成大写,不会修改原字符,返回一个修改后的字符串。
str1 = "hello music"
print(str1.capitalize())
print(str1)
lower
将所有大写字符转为小写,不会修改原字符,返回一个修改后的字符串。
str2 = "Hello MUSIC"
print(str2.lower()) #hello music
print(str2) #Hello MUSIC
upper
将所有小写字母转为大写,不会修改原字符,返回一个修改后的字符串。
str1 = "hello music"
print(str1.upper()) # HELLO MUSIC
print(str1) # hello music
title
把字符串的每个单词首字母大写,不会修改原字符,返回一个修改后的字符串。
str1 = "hello music"
print(str1.title()) #Hello Music
print(str1) #hello music
字符串判断
islower
检测字符串是否都是小写字母组成。True或False
str1 = "hello music"
#islower()检测字符串是否都由小写组成
print(str1.islower()) #True
isupper
检测字符串是否都是大写字母组成。True或False
#isupper()检测字符串是否都由大写组成
str1 = "hello music"
print(str1.isupper()) #False
isdigit
检测字符是否是数字。
#isdigit()是否是数字
count = 0
mystr = 'music 123'
for s in mystr:
if s.isdigit(): # mystr.isdigit() 返回值为True或False
count += 1 #统计是数字的个数
print(count) #包含数字的个数
startswith
判断字符串是否以某个字符或者字符串开头。
str1 = 'music 1123'
print(str1.startswith('c')) #False
print(str1.startswith('music')) #True
endswith
判断字符串是否以某个字符或者字符串结尾。
str1 = 'music 1123'
print(str1.endswith('1123')) #True
print(str1.endswith('3')) #True
print(str1.endswith('2')) #False
字符串删除
lstrip
删除字符串左侧的空白字符,不会修改原字符串,返回修改后的字符串。
str1 = ' music 1123'
print(str1.lstrip()) #music 1123
print(str1) # music 1123
rstrip
删除字符串右侧的空白字符,不会修改原字符串,返回修改后的字符串。
str1 = 'music 1123 '
print(str1.rstrip())
print(str1)
strip
删除字符串两端的空白字符,不会修改原字符串,返回修改后的字符串。
str1 = ' music 1123 '
print(str1.strip())
print(str1)