Python 基础

3.2 字符串的常见操作

2023-03-02  本文已影响0人  拖延症患者10

3.2.1 find

find:根据指定数据获取对应的下标。
提示:如果数据不存在,则返回-1,表示没有找到。

示例:

my_str = "abcdef"
index = my_str.find("c")
print("下标为:", index)     # 结果是:下标为: 2

新用法:find方法还可以指定数据范围,如果数据不存在,则返回-1,表示没有找到。

  1. __sub:要查找的数据
  2. __start:开始下标
  3. __end:结束下标(不包含结束下标)
index = my_str.find("c", 0, 2)
print("下标为:", index)     # 结果是:下标为: -1

扩展:

my_str = "hello"
# find 表示从左往右查找对应数据的下标
index = my_str.find("l")
print("下标为:", index)     # 结果是:下标为: 2
# rfind 表示从右往左查找对应数据的下标
index = my_str.rfind("l")
print("下标为:", index)     # 结果是:下标为: 3

3.2.2 index

index:根据指定数据获取对应的下标。
提示:如果数据不存在则崩溃。

示例:

 my_str = "hello"
 index = my_str.index("e")
 print("下标为:", index)     # 结果是:下标为: 1
 
 my_str = "hello"
 index = my_str.index("x")
 print("下标为:", index)      # 结果报错:ValueError: substring not found  即没有发现指定字符串

新用法:index方法还可以指定数据范围,如果数据不存在,则程序崩溃。

  1. __sub:要查找的数据
  2. __start:开始下标
  3. __end:结束下标(不包含结束下标)
index = my_str.index("l", 0, 3)
print("下标为:", index)     # 结果是:下标为: 2

3.2.3 count

count:根据指定数据统计该数据出现的次数。

示例:

my_str = "abccba"
result = my_str.count("a")
print("次数为:", result)     # 结果是:次数为: 2

新用法:count方法还可以指定数据范围。

  1. x:要统计的数据
  2. __start:开始下标
  3. __end:结束下标(不包含结束下标)
result = my_str.count("a", 0, 2)
print("次数为:", result)     # 结果是:次数为: 1

3.2.4 replace

replace:根据指定数据对字符串的数据进行替换。
提示:replace方法调用后会返回一个替换后的字符串。

示例:

  1. __old:旧的字符串
  2. __new:新的字符串(替换后的字符串)
my_str = "hello"
new_str = my_str.replace("l", "x")
print("替换后新的字符串为:", new_str)     # 结果是:替换后新的字符串为: hexxo
  1. __old:旧的字符串
  2. __new:新的字符串(替换后的字符串)
  3. __count:表示替换的次数,不指定表示全部替换
new_str = my_str.replace("l", "x", 1)
print("替换后新的字符串为:", new_str)     # 结果是:替换后新的字符串为: hexlo

3.2.5 split

split:根据指定数据对字符串进行分割。
分割次数:默认是-1,表示全部分割,1表示分割一次。

示例:

  1. sep:根据指定数据进行分割,返回的是一个列表数据
  2. maxsplit:表示最大的分割次数,不指定表示全部分割
my_str = "A:B:C"
result = my_str.split(":", 1)
print(result, type(result))     # 结果是:['A', 'B:C'] <class 'list'>

split扩展:不指定参数表示可以根据空白字符串进行分割(空白字符串:空格,\n, \t)。

my_str = "a b\nc\td"
result = my_str.split()
print(result, type(result))     # 结果是:['a', 'b', 'c', 'd'] <class 'list'>

3.2.6 startswith(endswith)

startswith(endswith):表示判断是否是以指定的字符串开头(结尾)。

示例:

my_str = "http://www.baidu.com"
  • startswith:表示判断是否是以指定的字符串开头。
result = my_str.startswith("http")
print(result)           # 结果是:True
  • endswith:表示判断是否是以指定字符串结尾。
result = my_str.endswith("cn")
print(result)           # 结果是:False

result = my_str.endswith("com")
print(result)           # 结果是:True

3.2.7 strip(lstrip、rstrip)

striplstriprstrip):去除两边(左边、右边)。
空白字符:空格、\n、\t

示例:

my_str = "   abc   "
print(my_str)         # 结果是:   abc
  • strip:去除两边空格
result = my_str.strip()
print(result)         # 结果是:abc
  • lstrip:只去除左边的空格
result = my_str.lstrip()
print(result)         # 结果是:abc
  • rstrip:只去除右边的空格
result = my_str.rstrip()
print(result)         # 结果是:   abc

扩展:strip还可以去除两边的指定数据

my_str = "!abc!"
new_str = my_str.strip("!")
print(new_str)         # 结果是:abc

3.2.8 partition

partition:表示根据指定数据把字符串分割成三部分。

my_str = "1.txt"
result = my_str.partition(".")
print(result, type(result))         # 结果是:('1', '.', 'txt') <class 'tuple'>

rpartition:表示根据指定数据从右往左找指定的数据把字符串分割成三部分。

my_str = "1.txt.png"
result = my_str.rpartition(".")
print(result, type(result))         # 结果是:('1.txt', '.', 'png') <class 'tuple'>

3.2.9 isdigit

isdigit:表示判断字符串里面的数据是否都是整型数据。

my_str = "80"
result = my_str.isdigit()
print(result)         # 结果是:True

3.2.10 join

join:根据指定数据把容器类型中的每一个数据按照字符串进行拼接,返回一个新的字符串。

示例:

my_str = "abc"
result = "_".join(my_str)
print(result, type(result))         # 结果是:a_b_c <class 'str'>

使用join还可以对列表中的每个数据按照字符串的方式进行拼接。

my_list = ["1", "2", "3"]
result = "#".join(my_list)
print(result, type(result))         # 结果是:1#2#3 <class 'str'>

提示:使用join方法的时候,容器类型中的每个数据必须是字符串类型才可以,因为返回的是一个字符串。

上一篇 下一篇

猜你喜欢

热点阅读