Python:字符串操作

218 阅读4分钟

字符串介绍

让我们再次回顾一下字符串的定义:凡是用单/双引号引起的都是str类型。

案例1:

name='hello'

下标(索引):

  1. 当是非负数的时候,是默认从0开始,从左向右依次数过去,所以这个下标从左往右数,最大数是4,即下标4对应的数是“o”。
  2. 当是负数的时候,默认从-1开始。
name='hello'
str1='python'
print(name[4],type(name[4]))
print(str1[-1],type(str1[-1]))
print(str1[-6],type(str1[-6]))

案例1结果:

image.png

切片——找到连续多个元素

正向切片的格式→对象[起始值:结束值] 负向切片的格式→对象[起始值:结束值:-步长]

注意:结束值是不包含在里面的

案例2:

str1='python'
print(str1[4])
print(str1[2:4])
print(str1[1:5:2])
print(str1[-2:-4:-1])
print(str1[::-1])
print(str1[::-2])

案例2结果:

image.png

由以上结果可以看出正向切片和负向切片还是有很多不同之处的:

  1. 正向切片只有1个冒号,负向切片有2个冒号;
  2. 负向切片最后一个值前面一定有个“-”代表方向,是从后往前的。

正向切片和负向切片表示方式的相同之处

  1. 都用中括号括起来;
  2. 中括号前面都有对象;
  3. 都用冒号来表示。

[M,N,S]

M是起始值,N是结束值,S是步长,如果为正,从前往后;如果为负,从后往前。

案例3:有一字符串str3='123456789',请拿出str3中所有偶数

str3='123456789'
print(str3[1:9:2])        #正向切片
print(str3[-2:-9:-2])     #负向切片

案例3结果:

image.png

字符串的操作:不可变类型

字符串的替换函数:replace()

字符串的操作什么叫不可变类型:通俗来讲,就是将数据的地址变了,但内容是不会变的。

str4='hello'
print(str4.replace('l', 'o'))
print(str4.replace('l', 'o',1))      #replace()函数
print(str4.replace('l', 'o',8))

案例4结果:

image.png

由案例4学到了replace()替换函数,其中replace()的格式如下:

replace(旧元素,新元素,替换次数)

所以才有案例4的结果,当替换次数不写的时候默认全部替换,当替换次数超过了当前元素的数量时,也会全部替换。

字符串的切割函数:split()

split():将字符串按指定符号进行切割,最终保存为列表

案例5:

str5 = '234345432324'
print(str5.split('4'))

案例5结果:

image.png

注意:最后的结果是有一个“ ”的,因为以'4'为切割符号进行字符串的切割时,当切割的左边或者右边没有元素时,需要用空格来代替。

字符串的去除函数:strip()

strip():去除字符串两端前后指定的符号,默认为空格。

lstrip():去除字符串左边指定的符号,默认为空格。

rstrip():去除字符串右边指定的符号,默认为空格。

案例6:

str6 = '    I AM Peric  '
str_6 = 'python,java'
print(str6.strip(' '))
print(str6.lstrip(' '))
print(str6.rstrip(' '))
print(str_6.strip('va'))

案例6结果:

image.png

join()函数

案例7:

S7='hello'
L7=['1','2','3']    #L7=['1',2,'3']就不行,里面必须是字符串形式
S_7='----'
print(S_7.join(S7))
print(S_7.join(L7))

案例7结果:

image.png

find()函数和index()函数

案例8

R8='hellosdjfhgjhgjyeroqwlkk'
print(R8.find('i'))     #查找不到返回-1
# print(R8.index('i'))  #查找不到报错
print(R8.find('h'))
print(R8.find('l',1))   #从下标3开始查找,查找字符串中第一个出现的字串
print(R8.index('o',5))  #从下标5开始查找,查找字符串中第一个出现的字串

案例8结果:

image.png

count()函数

count()函数:统计指定元素在字符串中出现的次数,找不到也不会报错。

案例9:

R8='hellosdjfh6546yeroqwlkk'
print(R8.count('6'))
print(R8.count('6',11))   #从下标11开始往后查找,统计6出现的次数
print(R8.count('p'))      #没出现p,报0次

案例9结果:

image.png

title()函数

案例10:

N10='hello world Python and JAVA and 123456'
print(N10.title())  #单词首字母大写,其余小写
print(N10.upper())  #单词全部大写
print(N10.lower())  #单词全部小写

案例10结果:

image.png

其余一些常用函数

案例11:

M11='hello'
M_11='hello@123456@world'
m11=' hello world'
m_11='helloworld'
m12='    '
print(M11.endswith('oo'))  #以...结尾
print(M11.startswith('hel')) #以...开头
print(M_11.isalpha())          #判断全是字母
print(M_11.isdigit())          #判断全是数字
print(M_11.isalnum())          #判断全是数字和字母
print(M_11.isalnum())
print(m_11.isalnum())
print(m11.isspace())          #判断是否全为空字符
print(m12.isspace())          #判断是否全为空字符
print(m_11.capitalize())      #只将字符串第一个字符转为大写,title()是全部字符首字母大写

案例11结果:

image.png

in和not in

案例12:

print('t' in 'hello')
print('t' not in 'hello')
print('l' in 'hello')
print('l' not in 'hello')

案例12结果:

image.png