一、字符串 str 常用方法
| 分类 |
方法 |
作用 |
示例 |
| 大小写 |
.upper() |
全部转大写 |
"abc".upper() → "ABC" |
|
.lower() |
全部转小写 |
"ABC".lower() → "abc" |
|
.title() |
单词首字母大写 |
"hi py".title() → "Hi Py" |
| 判断类 |
.isdigit() |
判断是否全为数字 |
"123".isdigit() → True |
|
.isalpha() |
判断是否全为字母 |
"abc".isalpha() → True |
|
.startswith(x) |
是否以x开头 |
"py".startswith("p") → True |
|
.endswith(x) |
是否以x结尾 |
"txt".endswith("t") → True |
| 查找 |
.find(x) |
找下标(找不到返回-1) |
"abc".find("b") → 1 |
|
.index(x) |
找下标(找不到报错) |
"abc".index("a") → 0 |
|
.count(x) |
统计x出现次数 |
"aab".count("a") → 2 |
| 切割拼接 |
.split(sep) |
按分隔符拆成列表 |
"1,2".split(",") → ["1","2"] |
|
.join(列表) |
用自身拼接列表元素 |
",".join(["1","2"]) → "1,2" |
| 替换清洗 |
.replace(a,b) |
把a替换成b |
"a1".replace("1","2") → "a2" |
|
.strip() |
去除首尾空格/换行 |
" py ".strip() → "py" |
| 截取 |
切片 [start:end:step] |
截取子串 |
"abcd"[1:3] → "bc" |
二、列表 / 数组 list 常用方法
| 分类 |
方法 |
作用 |
示例 |
| 增 |
.append(x) |
末尾加1个元素 |
[1,2].append(3) → [1,2,3] |
|
.insert(下标,x) |
指定位置插入 |
[1,3].insert(1,2) → [1,2,3] |
|
.extend(列表) |
合并另一个列表 |
[1].extend([2,3]) → [1,2,3] |
| 删 |
.pop(下标) |
按下标删,返回元素 |
[1,2].pop(0) → 1 |
|
.remove(x) |
按值删第一个匹配项 |
[1,2,1].remove(1) → [2,1] |
|
.clear() |
清空所有元素 |
[1,2].clear() → [] |
| 查 |
.index(x) |
返回元素下标 |
[1,2].index(2) → 1 |
|
.count(x) |
统计元素个数 |
[1,1,2].count(1) → 2 |
| 排序反转 |
.sort() |
原地升序排序 |
[3,1,2].sort() → [1,2,3] |
|
.reverse() |
原地反转列表 |
[1,2].reverse() → [2,1] |
| 其他 |
切片 [start:end] |
截取子数组 |
[1,2,3][1:] → [2,3] |
三、通用共用操作(字符串+列表都能用)
| 操作 |
作用 |
len(obj) |
获取长度:len("abc") / len([1,2]) |
x in obj |
判断是否包含:"a" in "abc" / 1 in [1,2] |
obj[i] |
通过下标取值 |
+ |
拼接:"a"+"b" / [1]+[2] |
四、关键区别(必记)
- 字符串不可改 :不能
s[0]='x';列表可改 :lst[0]=9 没问题
- 字符串方法都返回新字符串 ,原内容不变;列表多数方法是原地修改,无返回值
字符串
1.1 upper() 全部转大写
python
复制代码
s = "hello python"
res = s.upper()
print(res) # 输出:HELLO PYTHON
1.2 lower() 全部转小写
python
复制代码
s = "HELLO PYTHON"
res = s.lower()
print(res) # 输出:hello python
1.3 title() 单词首字母大写
python
复制代码
s = "hello python"
res = s.title()
print(res) # 输出:Hello Python
2.1 isdigit() 判断是否全为纯数字
python
复制代码
s1 = "12345"
s2 = "123a"
print(s1.isdigit()) # True
print(s2.isdigit()) # False
2.2 isalpha() 判断是否全为纯字母
python
复制代码
s1 = "abc"
s2 = "abc1"
print(s1.isalpha()) # True
print(s2.isalpha()) # False
2.3 startswith() 判断是否以指定内容开头
python
复制代码
s = "python.txt"
print(s.startswith("py")) # True
print(s.startswith("txt")) # False
2.4 endswith() 判断是否以指定内容结尾
python
复制代码
s = "python.txt"
print(s.endswith(".txt")) # True
print(s.endswith(".jpg")) # False
3.1 find() 查找下标(找不到返回-1)
python
复制代码
s = "abcdef"
print(s.find("c")) # 2
print(s.find("z")) # -1
3.2 index() 查找下标(找不到直接报错)
python
复制代码
s = "abcdef"
print(s.index("d")) # 3
# print(s.index("z")) # 报错
3.3 count() 统计字符出现次数
python
复制代码
s = "aabbaacc"
print(s.count("a")) # 4
print(s.count("b")) # 2
4.1 split() 按分隔符拆分字符串为列表
python
复制代码
s = "1,2,3,4"
lst = s.split(",")
print(lst) # ['1', '2', '3', '4']
4.2 join() 用字符串拼接列表元素
python
复制代码
lst = ["1","2","3"]
s = "-".join(lst)
print(s) # 1-2-3
5.1 replace() 替换指定内容
python
复制代码
s = "hello world"
res = s.replace("world","python")
print(res) # hello python
5.2 strip() 去除首尾空格/换行符
python
复制代码
s = " python "
res = s.strip()
print(res) # python
6. 字符串切片(通用取值)
python
复制代码
# 定义测试字符串
s = "ABCDEFG"
# 正向索引:0 1 2 3 4 5 6
# 反向索引:-7 -6 -5 -4 -3 -2 -1
# 1. 基础切片 [start:end]
print("1. 基础切片[1:4]:", s[1:4]) # BCD
# 2. 从开头截取到指定位置 [:end]
print("2. 开头到索引3:", s[:3]) # ABC
# 3. 从指定位置截取到末尾 [start:]
print("3. 索引2到末尾:", s[2:]) # CDEFG
# 4. 复制整个字符串 [:]
print("4. 完整复制字符串:", s[:]) # ABCDEFG
# 5. 指定步长截取 [start:end:step]
print("5. 步长为1全取:", s[0:7:1]) # ABCDEFG
print("5. 步长为2隔位取:", s[::2]) # ACEG
# 6. 负数索引(从右计数)
print("6. 最后1个字符:", s[-1]) # G
print("6. 倒数第3到倒数第1:", s[-3:-1]) # EF
# 7. 负步长:反转字符串
print("7. 整体反转字符串:", s[::-1]) # GFEDCBA
# 8. 倒序区间截取
print("8. 倒序区间截取[5:0:-1]:", s[5:0:-1]) # FEDCB
# 9. 跳过前N个字符
print("9. 跳过前3个字符:", s[3:]) # DEFG
# 10. 截取最后N个字符
print("10. 截取最后3个字符:", s[-3:]) # EFG
列表
1.1 append() 末尾追加单个元素
python
复制代码
lst = [1,2,3]
lst.append(4)
print(lst) # [1, 2, 3, 4]
1.2 insert() 指定下标插入元素
python
复制代码
lst = [1,3,4]
lst.insert(1,2)
print(lst) # [1, 2, 3, 4]
1.3 extend() 合并另一个列表
python
复制代码
lst1 = [1,2]
lst2 = [3,4]
lst1.extend(lst2)
print(lst1) # [1, 2, 3, 4]
2.1 pop() 按下标删除,返回被删元素
python
复制代码
lst = [1,2,3]
num = lst.pop(1)
print(num) # 2
print(lst) # [1, 3]
2.2 remove() 按值删除第一个匹配元素
python
复制代码
lst = [1,2,1,3]
lst.remove(1)
print(lst) # [2, 1, 3]
2.3 clear() 清空整个列表
python
复制代码
lst = [1,2,3]
lst.clear()
print(lst) # []
3.1 index() 返回元素首次出现下标
python
复制代码
lst = [2,5,8]
print(lst.index(5)) # 1
3.2 count() 统计元素出现次数
python
复制代码
lst = [1,1,2,2,2]
print(lst.count(2)) # 3
4.1 sort() 原地升序排序
python
复制代码
lst = [5,2,9,1]
lst.sort()
print(lst) # [1, 2, 5, 9]
4.2 reverse() 原地反转列表
python
复制代码
lst = [1,2,3,4]
lst.reverse()
print(lst) # [4, 3, 2, 1]
python
复制代码
lst = [10,20,30,40,50]
print(lst[2:4]) # [30, 40]
print(lst[::-1]) # [50, 40, 30, 20, 10]
字符串 & 列表 通用操作
1. len() 获取长度
python
复制代码
s = "python"
lst = [1,2,3]
print(len(s)) # 6
print(len(lst)) # 3
2. in 判断包含关系
python
复制代码
print("py" in "python") # True
print(2 in [1,2,3]) # True
3. + 拼接
python
复制代码
print("a"+"b") # ab
print([1]+[2,3]) # [1, 2, 3]
重点记忆
- 字符串不可变:所有方法返回新字符串,原字符串不变;
- 列表可变:增删改排序大多是原地修改,不返回新列表。