字符串是 Python 中最常用的数据类型之一,无论是处理文本数据、用户输入还是进行数据清洗,都离不开对字符串的操作。今天我们就来系统地学习 Python 字符串的定义、各类操作方法以及实际应用场景。

一、字符串的定义与索引取值

字符串是由字符组成的不可修改的序列,在 Python 中可以用单引号、双引号或三引号来定义。

1. 基本定义

my_str = "fanfandawang and awei"

2. 下标索引取值

字符串的每个字符都有一个下标(索引),从0开始计数;也可以从后往前数,最后一个字符下标是-1。

value = my_str[2]  # 取下标为2的字符
value2 = my_str[-10]  # 从后往前数,下标为-10的字符(空格也占一个下标哦)
print(f"从字符串{my_str}取下标为2的元素,值是:{value}")
print(f"从字符串{my_str}取下标为-10的元素,值是:{value2}")

小贴士:利用下标可以精准获取字符串中的单个字符,这在数据提取场景中很实用~

二、字符串的核心方法大揭秘

1. 查找方法:index()

index()方法用于查找子字符串在原字符串中的起始下标,若不存在则会抛出异常。

index = my_str.index("and")
print(f"在字符串{my_str}中查找and,其结果是:{index}")

2. 替换方法:replace()

replace(旧子串, 新子串)可以将字符串中的旧子串替换为新子串,返回替换后的新字符串(原字符串不变,因为字符串不可修改)。

new_str = my_str.replace("an", "花朵")
print(f"将字符串{my_str}进行替换后,得到的结果为:{new_str}")

3. 分割方法:split()

split(分隔符)会按照指定分隔符将字符串分割成列表。

my_str = "hello python itheima itcast"
my_str_list = my_str.split(" ")  # 按空格分割
print(f"字符串{my_str}进行切割后的结果是:{my_str_list},类型是{type(my_str_list)}")

4. 规整操作:strip()

strip()用于去除字符串首尾的空格;如果传入参数(如strip(“12”)),则会去除首尾指定的字符。

my_str = "  itheima and itcast  "
new_my_str = my_str.strip()  # 去除首尾空格
print(f"字符串{my_str}被strip后的结果是{new_my_str}")

my_str = "12itheima and itcast21"
new_my_str = my_str.strip("12")  # 去除首尾的"1"和"2"
print(f"字符串{my_str}被strip('12')后的结果是{new_my_str}")

5. 统计方法:count()与len()

count(子串):统计子串在字符串中出现的次数。
len(字符串):统计字符串的长度(字符个数)。

my_str = "itheima and itcast"
count = my_str.count("it")
print(f"字符串{my_str}中it出现的次数是:{count}")

num = len(my_str)
print(f"字符串{my_str}的长度是:{num}")

三、字符串的遍历:逐个字符的访问

遍历字符串就是逐个访问其中的每个字符,常用while循环和for循环实现。

1. while循环遍历

my_str = "饭饭大王历险记"
index = 0
while index < len(my_str):
    print(my_str[index])
    index += 1

2. for循环遍历

my_str = "饭饭大王历险记"
for x in my_str:
    print(x)

四、实战案例:字符串操作综合运用

我们通过一个案例来巩固以上知识点:统计字符串中特定子串的数量、替换分隔符并分割。

my_str = "itheima itcast boxuegu"
# 统计"it"出现的次数
num = my_str.count("it")
print(f"字符串{my_str}中有{num}个it字符")

# 将空格替换为"|"
new_my_str = my_str.replace(" ", "|")
print(f"字符串{my_str}被替换空格后,结果是:{new_my_str}")

# 按"|"分割成列表
my_str_list = new_my_str.split("|")
print(f"字符串{new_my_str}分割后的结果是:{my_str_list}")
操作类型 方法/操作 功能说明 示例代码
索引取值 下标索引([index] 通过正/负下标获取字符串中单个字符,正下标从0开始,负下标从-1开始(空格也占下标) my_str = "fanfandawang and awei"
value = my_str[2]
value2 = my_str[-10]
查找 index(sub) 查找子串sub在字符串中首次出现的起始下标,若不存在则抛异常 index = my_str.index("and")
替换 replace(old, new) 将字符串中所有old子串替换为new子串,返回新字符串(原字符串不变) new_str = my_str.replace("an", "花朵")
分割 split(sep) 按分隔符sep将字符串分割成列表,sep默认是空格 my_str = "hello python itheima itcast"
my_str_list = my_str.split(" ")
规整(去首尾) strip([chars]) 若不传入chars,去除字符串首尾空格;若传入chars,去除首尾指定字符 my_str = " itheima and itcast "
new_my_str = my_str.strip()
my_str = "12itheima and itcast21"
new_my_str = my_str.strip("12")
统计次数 count(sub) 统计子串sub在字符串中出现的次数 my_str = "itheima and itcast"
count = my_str.count("it")
统计长度 len(str) 统计字符串的字符个数(包含空格等) num = len(my_str)
遍历 while循环/for循环 逐个访问字符串中的每个字符 # while循环
my_str = "饭饭大王历险记"
index = 0
while index < len(my_str):
print(my_str[index])
index += 1
# for循环
for x in my_str:
print(x)

五、总结

字符串作为 Python 中处理文本的核心工具,掌握其定义和操作方法(索引、查找、替换、分割、规整、统计、遍历等)是每位 Python 学习者的必修课。从简单的字符提取到复杂的文本处理,字符串方法总能帮你高效完成任务。
快去动手实践这些方法吧,相信你会在文本处理的路上越走越顺!

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐