0
点赞
收藏
分享

微信扫一扫

每天 3 分钟,小闫带你学 Python(十)

夹胡碰 2022-03-12 阅读 52

 

正文共:4820 字 6 图

预计阅读时间:13分钟

每天 3 分钟,小闫带你学 Python(十)_搜索

每日分享

Always remember that the future comes one day at a time.

要永远记住,未来会一天一天到来。

小闫语录:

不要以为未来很远,每一个明天都是未来。


每天 3 分钟,小闫带你学 Python(十)_搜索_02

前言

每天 3 分钟,小闫带你学 Python(十)_字符串_03

昨天的文章『​​每天 3 分钟,小闫带你学 Python(九)​​』讲了字符串的一些概念,有字符串的输出和输入,以及下标索引和切片。​下标索引即取出字符串的某一个元素,而切片则是取出一部分内容。

提一个小知识点,Numbers 类型中有长整型 ​long​ ,如果在 python3 中输入下方代码会报错:

num = 11111L

已知 python3.6 以及 python 3.7 会报错,其他 python3.x 未进行实验。实验 python2.7 未报错。

后来查资料了解到,python2.x 是可以使用的,而 python3.x 简化了这种写法,末尾不需要写 ​L​ 进行标明,python 会根据你数字大小自动进行转换整型和长整型。

1.字符串的常见操作

了解了字符串的相关概念以后,我们需要学习字符串的相关操作,即它的一些方法。字符串类型是重中之重,必须加以重视。

首先我们定义一个字符串:

mystr = "My name is EthanYan"

下面所有的例子都使用其进行操作。

1.1 find

检测某个字符串是否包含在 ​mystr​ 中。如果是,返回开始的索引值,否则返回-1

In [1]: mystr = "My name is EthanYan"In [2]: mystr.find("Ethan")Out[2]: 11In [3]: mystr.find("E")                                                 Out[3]: 11 In [4]: mystr.find("z")Out[4]: -1

上述界面是 ​ipython​ 交互环境,感兴趣的同学可以上网查一下。 ​in​ 代表输入代码, ​out​ 代表输出结果。

我们可以看到返回了索引值,而且返回的是 ​Ethan​ 中 ​E​ 的索引值。如果找一个不存在的元素,返回 -1。其实 ​find​ 方法内部还可以添加参数:

mystr = "My name is EthanYan"print(mystr.find('name', 0, 10))

其中 name 后方第一个参数代表开始搜索位置处索引,而第二个参数代表结束搜索位置处索引。即从 ​mystr​ 中下标为0处到下标为10处之间是否包含 ​name​ ,有则返回其起始索引值。

如果不写起始位置与结束位置,代表搜索整个字符串。


1.2 rfind

与 ​find​ 一样,只是从右往左进行搜索

# mystr = "My name is EthanYan"In [5]: mystr.rfind("Ethan")Out[5]: 11In [6]: mystr.rfind("a")Out[6]: 17In [7]: mystr.find("a")Out[7]: 4In [8]: mystr.rfind("z")Out[8]: -1

mystr​ 中包含两个 ​a​ ,我们可以看到 ​rfind​ 返回的是从右往左进行搜索的结果,从而得到验证。

1.3 index

语法为:

mystr.index(str, start=0, end=len(mystr))

解读:​从 ​mystr​ 中搜索 ​str​ 。其中 ​len 为一个可以计算对象长度的函数​,上方 ​len(mystr)​ 的含义是看 ​mystr​的结束位置索引值。如果不写起始位置与结束位置代表搜索整个字符串。

它与 ​find​ 类似,但有一点不同,​如果找不到会报错​。

mystr = "My name is EthanYan"print(mystr.index('z'))

报错信息为:

ValueError: substring not found

搜索包含的字符串:

mystr = "My name is EthanYan"print(mystr.index('a'))

结果返回其索引值 ​4​。

1.4 rindex

与 ​index​ 相同,只是从右往左进行搜索。此处不举例。

1.5 count

返回 ​str​ 在 ​mystr​ 中的 ​start​ 和 ​end​ 之间出现次数。

mystr.count(str, start=0, end=len(mystr))

示例:

mystr = "My name is EthanYan"print(mystr.count('a'))

输出结果为:

3

1.6 replace

把 ​mystr​ 中的 ​str1​ 替换成 ​str2​,如果 ​count​ 指定,则替换 ​count​次,但是不超过 ​mystr.count(str1)​次。(比如 ​mystr​ 中有 3 个 ​a​,最多替换 3 次,不能替换 4 次,因为没有那么多 ​a​ 让你去替换。)

mystr.replace(str1, str2, count)

示例为:

mystr = "My name is EthanYan"print(mystr.replace('a', 'x', 4))

将 ​mystr​ 中所有的 ​a​ 替换为 ​x​ ,因为只有3个 ​a​ ,因此只替换 3 次。结果如下:

My nxme is EthxnYxn

如果替换两次,那么会是下面的结果:

My nxme is EthxnYan

只替换两次,前两个 ​a​ 被替换掉。

1.7 split

以 ​str​ 为分隔符切片 ​mystr​,如果 ​maxsplit​ 有指定值,则仅分隔 ​maxsplit​ 次。(比如 ​mystr​ 中包含 3 个空格,即使写 4,它只能分割 3 次)

mystr.split(str, maxsplit)

示例:

mystr = "My name is EthanYan"print(mystr.split(' ', 3))

注意字符串中有个一个空格,代表 ​mystr​ 按照空格进行分割,分割 3 次。

输出结果为:

['My', 'name', 'is', 'EthanYan']

应用场景:​将字符串转列表。

1.8 capitalize

把字符串的第一个字符大写

mystr = "my name is EthanYan"print(mystr.capitalize())

注意首字母的变化

输出结果为:

My name is ethanyan

1.9 startswith

检查字符串是否是以某一字符串开头, 是则返回 True,否则返回 False

mystr = "My name is EthanYan"print(mystr.startswith('My'))

结果为:

True

1.10 endswith

检查字符串是否以某一字符串结尾,如果是返回True,否则返回 False

mystr = "My name is EthanYan"print(mystr.endswith('Yan'))

输出结果为:

True

1.11 lower

转换 ​mystr​ 中所有大写字符为小写

mystr = "My name is EthanYan"print(mystr.lower())

结果为:

my name is ethanyan

1.12 upper

转换 ​mystr​ 中的小写字母为大写

mystr = "My name is EthanYan"print(mystr.upper())

输出结果为:

MY NAME IS ETHANYAN

1.13 ljust

返回一个原字符串左对齐,并使用空格填充至长度 ​width​ 的新字符串

>>> mystr = "My name is EthanYan">>> mystr.ljust(23)'My name is EthanYan    '

我们可以明显看到返回的结果末尾多了一些空格。

上方的代码显示界面来自于 python 自带的解释器。

1.14 rjust

返回一个原字符串右对齐,并使用空格填充至长度 ​width​ 的新字符串

>>> mystr = "My name is EthanYan">>> mystr.rjust(23)'    My name is EthanYan'

可以明显看到右对齐,并且有空格填充的场景。

1.15 center

返回一个原字符串居中,并使用空格填充至长度 ​width​ 的新字符串

>>> mystr = "My name is EthanYan">>> mystr.center(23)'  My name is EthanYan  '

1.16 lstrip

删除 ​mystr​ 左边的空白字符

>>> mystr = "     My name is EthanYan">>> mystr.lstrip()'My name is EthanYan'

1.17 rstrip

删除 ​mystr​ 字符串末尾的空白字符

>>> mystr = "My name is EthanYan       ">>> mystr.rstrip()'My name is EthanYan'

1.18 strip

删除 ​mystr​ 字符串两端的空白字符

>>> mystr = "   My name is EthanYan       ">>> mystr.strip()'My name is EthanYan'

1.19 title

把字符串的每个单词首字母大写

mystr = "my name is EthanYan"print(mystr.title())

输出结果为:

My Name Is Ethanyan

1.20 partition

把 ​mystr​ 以 ​str​ 分割成三部分, ​str​ 前, ​str​ 和 ​str​ 后

>>> mystr = "My name is EthanYan">>> mystr.partition('name')('My ', 'name', ' is EthanYan')

1.21 rpartition

类似于 ​partition()​ 函数,不过是从右边开始

>>> mystr = "My name is EthanYan">>> mystr.rpartition('a')('My name is EthanY', 'a', 'n')>>> mystr = "My name is EthanYan">>> mystr.partition('a')('My n', 'a', 'me is EthanYan')

1.22 splitlines

按照行分隔,返回一个包含各行作为元素的列表

>>> mystr = "My name is EthanYan\n nice ~">>> mystr.splitlines()['My name is EthanYan', ' nice ~']

\n​ 是换行符。可以看到将数据按行进行分割,分别作为列表的元素。

1.23 isalpha

如果 ​mystr​ ​所有字符都是字母​,则返回 ​True​,否则返回 ​False

>>> mystr = "My name is EthanYan">>> mystr.isalpha()False>>> mystr = "MynameisEthanYan">>> mystr.isalpha()True>>> mystr = "MynameisEthanYan你好">>> mystr.isalpha()True

第一个例子中包含了空格,返回 ​False​;第二个例子中只包含字母,返回 ​True​;第三个例子中注意一个问题,如果字符串包含汉字,那么也会将其算作字母。

1.24 isdigit

如果 ​mystr​ ​只包含​数字则返回 ​True​ 否则返回 ​False

>>> mystr = "My name is EthanYan">>> mystr.isdigit()False>>> mystr = "1111">>> mystr.isdigit()True

1.25 isalnum

如果 ​mystr​ 所有字符都是​字母或数字​则返回 ​True​,否则返回 ​False

>>> mystr = "My name is EthanYan666">>> mystr.isalnum()False>>> mystr = "MynameisEthanYan666">>> mystr.isalnum()True

1.26 isspace

如果 ​mystr​ 中只包含空格,则返回 ​True​,否则返回 ​False

>>> mystr = " ">>> mystr.isspace()True

1.27 join

mystr​ 中每个元素之间插入 ​str​ ,构造出一个新的字符串

>>> mystr = ["我", "你"]>>> "爱".join(mystr)'我爱你'

应用场景:​将列表转成字符串。

2.作业

今日​学习目标:​练习每一个方法,不要求全部记住,但要求全部练习过两遍。

1.练习字符串相关操作。



每天 3 分钟,小闫带你学 Python(十)_python_04


每天 3 分钟,小闫带你学 Python(十)_字符串_05

 

举报

相关推荐

0 条评论