Python字符串操作指南

图片[1]-Python字符串操作指南-山海云端论坛

01

引言

在Python中,字符串String是常用的数据类型之一,本文就字符串中相关常见的函数和操作进行汇总,方便大家查漏补缺。

闲话少说,我们直接开始吧!

02

计算字符串长度

一般来说字符串的长度指的是字符串中所包含的字符的数量。如果我们需要得到字符串的长度,我们一般使用内置的函数 len,如下所示:

<code>fruit = "apple"</code><code>print(len(fruit))</code><code><em># 5</em></code>

03

计算字符串中子串数目一般来说,我们也可以使用函数 count() 来计算字符串中相应子串的数目,如下所示:

<code>string = "aaaabbbcc"</code><code>print(string.count("a")) # 4, as "a" appears 4 times</code><code>print(string.count("b")) # 3, as "b" appears 3 times</code><code>print(string.count("c")) # 2</code><code>print(string.count("z")) # 0</code><code>print(string.count("aa")) # 2</code>

04

 字符串中查找子串1) 函数Index一般来说,我们使用函数 index 来获取子串在字符串中的索引位置,样例如下:

<code>string = "abcdefg"</code><code>print(string.index("a")) # 0</code><code>print(string.index("b")) # 1</code><code>print(string.index("c")) # 2</code><code>print(string.index("z")) # error</code>

需要明确的是函数 index 的输入参数为需要查找的子串,它遍历整个字符串来寻找是否包含相应的子串,如果可以找到,就返回该子串首次出现的位置。如果不存在,则将触发以下错误:

ValueError: substring not found

2) 函数find

函数 find 的功能和函数 index 的功能类似,不同点在于如果子串不存在,函数 find 不会触发错误而是返回 -1 ,举例如下:

<code>string = "abcdefg"</code><code>print(string.find("a")) <em># 0</em></code><code>print(string.find("b")) <em># 1</em></code><code>print(string.find("c")) <em># 2</em></code><code>print(string.find("z")) <em># -1</em></code>

3) 函数rfind

函数 rfind功能和 find 类似,但是该函数返回的是最后出现子串的位置索引而不是首次出现的位置索引。

举例如下:

<code>string = "abcabc"</code><code>print(string.rfind("a")) <em># 3 -> index of last-found "a"</em></code>

05

删除字符串中的空白字符这里空白字符主要包括以下三种:

‘ ‘    ->  空格字符 

‘\n’  ->  换行字符 

‘\t’   ->  水平制表符

1) 函数strip函数strip()主要用于删除字符串中左右两端的上述三种空白字符,举例如下:

<code>string = " \n\t apple \n\t\n "</code><code>print(string.strip()) # "apple"</code>

2) 函数lstrip函数 lstrip() 主要用于删除字符串中左端的上述三种空白字符,举例如下:

<code>string = " \n\t apple \n\t\n "</code><code>print(string.lstrip()) # "apple \n\t\n "</code>

3) 函数rstrip同时,函数 rstrip() 主要用于删除字符串中右端的上述三种空白字符,举例如下:

<code>string = " \n\t apple \n\t\n "</code><code>print(string.rstrip()) # " \n\t apple"</code>

06

使用特定字符分割字符串函数 split() 主要用于使用空格字符将字符串拆分成小的字符串列表,举例如下:

<code>string = "apple orange pear"</code><code>print(string.split()) # ["apple", "orange", "pear"]</code>

一般来说,函数 split() 默认使用空格来将字符串进行拆分,同时如果我们传入字符串参数,那么函数split() 将根据传入的内容来分割字符串,举例如下:

<code>string = "apple,orange,pear"</code><code>print(string.split(",")) # ["apple", "orange", "pear"]</code>

07

使用特定字符拼接字符串

函数 join 主要用于传入字符串列表,并对字符串列表中所有的字符串通过分隔符进行拼接,举例如下:

<code>list1 = ["apple", "orange", "pear"]</code><code>string1 = " ".join(list1) # "apple orange pear"</code><code>string2 = ",".join(list1) # "apple,orange,pear"</code><code>string3 = "!!!".join(list1) # "apple!!!orange!!!pear"</code>

08

处理字符串中的大小写

1) 函数 upper

函数 upper 主要用于将字符串中的所有小写字符转化为大写字符,如下所示:

<code>string = "apple"</code><code>string = string.upper() # "APPLE"</code>

2) 函数 lower

函数 lower 主要用于将字符串中的所有大写字符转化为小写字符,如下所示:

<code>string = "APPLE"</code><code>string = string.lower() # "apple"</code>

3) 函数 isupper

函数 isupper 主要用于判断字符串中所有字符均为大写字符时返回True,否则返回 False ,举例所示:

<code>string1 = "APPLE"</code><code>string2 = "Apple"</code><code>print(string1.isupper()) <em># True</em></code><code>print(string2.isupper()) <em># False</em></code>

4) 函数 islower

函数 islower 主要用于判断字符串中所有字符均为小写字符时返回True,否则返回 False,举例所示:

<code>string1 = "apple"</code><code>string2 = "Apple"</code><code>print(string1.islower()) <em># True</em></code><code>print(string2.islower()) <em># False</em></code>

5) 函数 title

函数 title 主要用于将字符串中每个单词的首个字母转化为大写,举例如下:

<code>sentence = "my name is bob"</code><code>print(sentence.title()) <em># My Name Is Bob"</em></code>

6) 函数 swapcase

函数 swapcase主要用于将字符串中每个字符的大写字符转化为小写,同时将其中的小写字符转化为大写,非字母字符保持不变,举例如下:

<code>string = "abCDefG"</code><code>print(string.swapcase()) # "ABcdEFg"</code>

09

判断字符串是否仅包含某些字符

1) 函数 isalnum

函数 isalnum 主要用于判断字符串中每个字符是否都是数字或者字母,举例如下:

<code>string1 = "abc123"</code><code>string2 = "abc123!"</code><code>print(string1.isalnum()) <em># True</em></code><code>print(string2.isalnum()) <em># False</em></code>

上述字符串 string2 返回 False 的原因主要是由于字符 ! 。

2) 函数 isalpha

函数 isalpha主要用于判断字符串中每个字符是否都是字母,如果存在一个非字母的字符则将返回False,举例如下:

<code>string1 = "abc"</code><code>string2 = "abc123"</code><code>print(string1.isalpha()) <em># True</em></code><code>print(string2.isalpha()) <em># False</em></code>

3) 函数 isdigit

函数 isdigit主要用于判断字符串中每个字符是否都是数字,如果存在一个非数字的字符则将返回 False,举例如下:

<code>string1 = "123"</code><code>string2 = "123a"</code><code>print(string1.isdigit()) <em># True</em></code><code>print(string2.isdigit()) <em># False</em></code>

4) 函数 isspace

函数 isspace 主要用于判断字符串中每个字符是否都是空白字符,如果存在一个非空白字符则将返回 False,举例如下:

<code>string1 = " \n\n\t\t "</code><code>string1 = " \n\n\t\t a"</code><code>print(string1.isspace()) <em># True</em></code><code>print(string2.isspace()) <em># False</em></code>

10

总结

本文重点介绍了Python中字符串string中常见的操作和相应的应用场景,并给出了相关代码示例。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容