在Python03中我们已经知道了字符串的声明和使用,以及字符串替换,分割两个操作。在计算机中字符串数据类型的时候无疑是最多且常见的,所以我们仅仅只会字符串的定义和声明及替换切割操作的话是不能很好的去处理我们的字符串数据类型的。现在我们去更进一步的了解字符串的相关常用操作。
目录
1, find函数
查找子串的位置,如果子串不存在,返回-1,如果存在则返回该字符串开头的字符所在的索引位置。需要注意的是,字符串的索引位置是从0开始的。
如上,我们可以看到,虽然hello加上空格有7个字符,但是我们的world字符串依旧是在索引位置为6的地方,可见,在计算机语言中字符串的索引一般是从0开始的。并且,如果我们不指定起始和终止的索引位置的话,find函数只会返回需要查找的字符串第一次出现的的索引位置。如果我们在里面输入start和end的索引位置的话,那么就会将后面出现的对应字符串索引位置返回。
if __name__ == '__main__':
content1 ="hello world!day day up!try to love world........."
print(content1.find("world")) # 返回第一次查找到字符串的索引位置
print(content1.find("world", 10 , len(content1)-3)) # 指定开始和结束的查找范围
print(content1.find("died")) # 查找主字符串中没有的子字符串
2,index函数
index 查找子串的位置,如果子串不存在,抛异常。index的使用和find类似,但是它不像find函数那样,找不到对应的子字符串时返回-1,而是抛异常。如下:
if __name__ == '__main__':
content1 ="hello world!day day up!try to love world........."
print(content1.index("world")) # 返回第一次查找到字符串的索引位置
print(content1.index("world", 10 , len(content1)-3)) # 指定开始和结束的查找范围
print(content1.index("died")) # 查找主字符串中没有的子字符串
3,startswith函数和endswith函数
startswith函数和endswith函数分别用于判断字符串是否以指定的子字符串开头或者是以指定的子字符串结尾,返回值为Boolean布尔类型的数据:
if __name__ == '__main__':
content1 ="hello world!day day up!try to love world........."
print(content1.startswith("hello")) # 查看是否以hello开头
print(content1.endswith("world")) # 查看是否以world结尾
4,join函数:字符串的拼接
该函数的使用有点类似于print函数中的sep属性的作用:使用指定的连接符将字符连接在一起,不过join函数分隔的是单个字符串,而我们的print函数可以是多个字符串。值得注意的是我们的连接符在前,join函数里面的参数传入我们要就行连接的字符串。
if __name__ == '__main__':
content1 ="hello world!day day up!try to love world........."
print("♠".join(content1))
5,字符串的切片
字符串的切片就类似于列表一样,指定开始和结束位置的索引,即可切出我们想要范围的内容。
1)str[start:end]
if __name__ == '__main__':
content1 ="hello world!day day up!try to love world........."
print(content1[content1.find("day"):content1.find("try")])
2)str[start:]
当我们不指定切到哪里结束时,默认到字符串的最后一个字符索引位置。
if __name__ == '__main__':
content1 ="hello world!day day up!try to love world........."
print(content1[content1.find("day"):])
3)str[start:-1]
当我们在结束的位置出写上-1后,结果和没有写结束位置的索引一样,也是从start的索引位置一直切到字符串的最后一个字符:
if __name__ == '__main__':
content1 ="hello world!day day up!try to love the world........."
print(content1[content1.find("day"):-1])
4)str[:]
当我们不指定开始和结束的索引位置时,默认切片是从字符串的第一个字符到最后一个字符为止:
if __name__ == '__main__':
content1 ="hello world!day day up!try to love the world........."
print(content1[:])
6,去除字符串中的空格
1)strip():
strip有剥去,去除的意思。在对字符串进行操作的时候,strip只是去除字符串首尾位置的空格,别的地方它无法去除:
if __name__ == '__main__':
content1 =" hello world! day day up! try to love the world......... "
print(content1)
content1_strip=content1.strip()
print(content1_strip)
2)lstrip()
lstrip函数不能像strip函数那样左右两边的空格都可以去除,对于lstrip函数来说,它只能够去除左边的空格:
if __name__ == '__main__':
content1 =" hello world! day day up! try to love the world......... "
print(content1)
content1_lstrip=content1.lstrip()
print(content1_lstrip)
3)rstrip()
rstrip函数和lstrip函数刚刚好相反,它去除的是右边的空格:
if __name__ == '__main__':
content1 =" hello world! day day up! try to love the world......... "
print(content1)
content1_rstrip=content1.rstrip()
print(content1_rstrip)
7,字符串对齐
1) center函数
center函数能够让我们的字符串居中显示,并可以指定长度以及使用符号来补齐左右两边。
如果指定长度>=原来字符串的长度,那么字符串对齐函数就会返回原来的函数,并字符串长度不够的地方使用符号来进行补全。
if __name__ == '__main__':
content1 =" hello world! day day up! try to love the world......... "
print(content1)
print(content1.center(100,"*")) # 字符串居中显示,并使用*补全左右
2)ljust函数
ljust函数可以让我们的字符串左对齐:
if __name__ == '__main__':
content1 =" hello world! day day up! try to love the world......... "
print(content1)
print(content1.ljust(100,"*")) # 字符串左对齐,并使用*补全右边
因为我们的字符串在左边自带多个空格,因此看起来像是我们的字符串和左边没有对齐一样。
3)rjust函数
rjust函数可以让我们的字符串右对齐,与中间对齐,左对齐函数一样,可以指定长度和符号。
if __name__ == '__main__':
content1 =" hello world! day day up! try to love the world......... "
print(content1)
print(content1.rjust(100,"*")) # 字符串右对齐,并使用*补全左边
8,字符串大小写转换
1)upper函数
upper函数可以将我们输入的英文字母都变成大写字母:
if __name__ == '__main__':
content1 ="aBcDeF"
print(content1.upper())
2)lower函数
lower函数可以将我们输入的英文字母都变成小写字母:
if __name__ == '__main__':
content1 ="aBcDeF"
print(content1.lower())
3)capitalize函数
capitalize函数可以将字符串中的第一个字母转换成大写形式,其后的字母全部变成小写格式:
if __name__ == '__main__':
content1 ="aBcDeF"
print(content1.capitalize())
4)title函数
title函数将字符串中每个单词的首字母转换成大写形式:
if __name__ == '__main__':
content1 ="aBcDeF hei"
print(content1.title())
以上就是字符串常用的操作,有问题请在评论区留言。