掌握字符串内建函数

字符串内建函数

字符串作为常用的一种数据类型,它提供了很多内建函数,接下来,列举一些字符串常用的函数。

1,find 函数

find 函数用于检测字符串中是否包含子字符串 sub,如果指定 start(开始)和 end (结束)范围,则检查是否包含在指定范围内。如果包含子字符串则返回开始的索引值,否则返回 -1。

语法格式如下:

str.find(sub[, start[, end]])

参数含义如下:
(1) sub:指定索引的字符串
(2)start:开始索引,默认为0
(3)end:结束索引,默认为字符串的长度

例如:查找 “itheima”是否在字符串 sting_example中,代如下:

string_example = 'hello word itheima and itheimaApp'
index = string_example.find("itheima")
print(index)

运行结果如下:

在这里插入图片描述

2, index 函数

index 函数检测字符串中是否包含子字符串 sub , 如果指定start(开始)和 end (结束)范围,则检查是否包含在指定的范围内。如果包含子字符串,则返回子字符串开始的索引值,否则抛出异常。

语法格式如下:

str.index(sub[, start[, end]])

参数含义如下:
(1) sub:指定检索的字符串
(2)start:开始索引,默认为0
(3)end:结束索引,默认为字符串的长度

通过下列案例演示 index 函数的使用:

string_example = 'hello word itheima and itheimaApp'
index = string_example.index("itheima", 0, 10)
print(index)

运行结果如下:(子字符串不在索引范围, 因此程序报错)
在这里插入图片描述

3,count 函数

count函数用于统计字符串 sub 字串出现的次数,可以设定开始与结束位置来限制字符串的搜索范围,该函数返回子字符串在字符串中出现的次数。

语法格式如下:

str.count(sub[, start[, end]])

参数含义如下:
(1)sub:搜索的子字符串
(2)start:字符串开始搜索的位置。默认为第一个字符,该字符串引值为0
(3)end:字符串结束搜索的位置。默认为字符串的长度

通过下列案例演示,代码如下:

string_example = 'hello word itheima and itheimaApp'
result = string_example.count("itheima")
print(result)

运行结果如下:
在这里插入图片描述

4,replace 函数

replace 函数把字符串中 old(旧字符串)替换成 new(新字符串),该函数返回的是字符串中old (旧字符串)替换成 new(新字符串)后生成的新字符串。如果指定第3个参数count,则替换不超过count 次。

语法格式如下:

str.replace(old, new[, count])

参数含义如下:
(1)old:将被替换的子字符串
(2)new:新字符串,用于替换 old 子字符串
(3)count:可选参数,替换不超过 count 次

通过下列案例演示 replace函数的使用:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.replace('itheima', 'ITHEIMA', 1)
print(new_string)

运行结果如下:
在这里插入图片描述

5,split 函数

split 函数通过指定分隔符对字符串进行切片,如果参数 maxsplit 有指定值,则仅分割 maxsplit 个字符串,该函数的返回值是分割后的字符串列表。

语法格式如下:

str.split(sep = None, maxsplit = -1)

参数含义如下:
(1)sep:分隔符,默认为所有空字符串,包括空格、换行(\n)、制表符(\t)等
(2)maxsplit:分割次数。

通过下列案例演示 split 函数的使用,代码如下:

string_example = 'this is string example ....wow!!!'
print(string_example.split())
print(string_example.split('i', 1))
print(string_example.split('w'))

运行结果如下:
在这里插入图片描述

6,capitalize 函数

capitalize 函数用于将字符串的第一个字母变成大写,其他字母变成小写,该函数返回一个首字母大写的字符串。

语法格式如下:

str.capitalize()

通过下列案例演示 capitalize 函数的使用,代码如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.capitalize()
print(new_string)

运行结果如下:
小脆筒style

7,title 函数

title 函数返回 “标题化” 的字符串,也就是说所有的单词都是以大写开始,其余字母均为小写。

语法格式如下:

str.title()

通过下列案例演示,代码如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.title()
print(new_string)

运行结果如下:
在这里插入图片描述

8,startswith 函数

startswith 函数用于检查字符串是否是以指定子字符串开头,如果是,则返回True,否则返回False. 若指定了start和end 参数的值,则会在指定的范围内检查。

语法格式如下:

str.startswith(prefix[, start[, end]])

参数含义如下:
(1) prefix:检测的字符串
(2)start:可选参数,用于设置字符串检测的起始位置
(3)end:可选参数,用于设置字符串检测的结束位置

通过下列案例演示startswith 函数的使用,代码如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.startswith('hello')
print(new_string)

运行结果如下:
在这里插入图片描述

9,endswith 函数

endswith 函数用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回Flase。可选参数start 与end为检索字符串的开始与结束位置。

语法格式如下:

str.endswith(suffix[, start[, end]])

参数含义如下:
(1)suffix:检测的字符串
(2)start:字符串中的开始位置
(3)end:字符串的结束位置

通过下列案例演示 endswith 函数的使用,代码如下:

string_example = 'hello word itheima and itheimaApp'
new_string_one = string_example.endswith('app')
new_string_two = string_example.endswith('App')

print(new_string_one)
print(new_string_two)

运行结果如下:

在这里插入图片描述

10,upper 函数

upper 函数将字符串中的小写字母转换为大写字母,返回小写字母转换为大写字母的字符串。

语法格式如下:

str.upper()

通过下列案例演示,代码如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.upper()
print(new_string)

运行结果如下:
在这里插入图片描述

11,ljust 函数

ljust 函数返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度,则返回原字符串。

语法格式如下:

str.ljust(width[, fillchar])

参数含义如下:
(1)width:指定字符串长度
(2)fillchar:填充字符,默认为空格

通过下列案例演示,代码如下:

old_string = 'hello word itheima and itheimaApp'
print(old_string.ljust(50, '*'))

运行结果如下:
在这里插入图片描述

12,rjust 函数

rjust 函数返回一个原字符串右对齐,并使用空格填充至长度为 width 的新字符串。如果指定的长度小于原字符串的长度,则返回原字符串。

语法格式如下:

str.rjust(width[, fillchar])

参数含义如下:
(1)width:填充指定字符串后字符串的总长度
(2)fillchar:填充的字符,默认为空格

通过下列案例演示 rjust 函数的使用,代码如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.rjust(50)
print(new_string)

运行结果如下:

在这里插入图片描述

13,center 函数

center 函数返回一个宽度为 width、原字符串居中、以 fillchar(默认为空格)填充左右两边的字符串。

语法格式如下:

str.center(width[, fillchar])

参数含义如下:
(1)width:字符串的总长度
(2)fillchar:填充字符

14,lstrip 函数

lstrip 函数用于截掉字符串左边的空格或指定字符,返回的是一个新字符串。

语法格式如下:

str.lstrip( [chars] )

参数含义如下:

chars:指定截取的字符

通过下列案例演示 lstrip 函数的使用,代码如下:

old_string_one = 'hello word itheima and itheimaApp'
old_string_two = '       hello word itheima and itheimaApp  '
new_string_one = old_string_one.lstrip()
new_string_two = old_string_two.lstrip()
print(new_string_one)
print(new_string_two)

运行结果如下:
在这里插入图片描述

15,rstrip 函数

rstrip 函数用于删除字符串末尾的指定字符,返回的是一个新的字符串。

语法格式如下:

str.rstrip([chars])

参数含义如下:
chars:指定删除的字符串,默认为空格。

16,strip 函数

strip 函数用于移除字符串头尾指定的字符,返回的是一个新字符串。

语法格式如下:

str.strip([chars])

参数含义如下:
chars:指定删除的字符串,默认为空格。

通过下列案列演示,代码如下:

old_string = '      hello word itheima and itheimaApp'
new_string = old_string.strip()
print(new_string)

运行结果如下:
在这里插入图片描述

补充: Unicode 字符串
1)在Python2 中,普通字符串是以8位 ASCII码进行储存的,而Unicode 子字符串中的字符则按照 Unicode 储存,这样能够表示更多的字符集。
2)在Python3 中,所有的字符串都是Unicode 字符串。

END
感谢看到这里的各位读者朋友们,如果你感到本文写的不错,就顺手点个赞收藏一下,也可以关注一波~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章