一、字符串 str,有序序列(支持下标)不可变
1.1 字符串的定义
字符串 就是 一串字符,是编程语言中表示文本的数据类型
在 Python 中可以使用 一对双引号 " 或者 一对单引号 ' 定义一个字符串
虽然可以使用 \" 或者 \' 做字符串的转义,但是在实际开发中:
如果字符串内部需要使用 ",可以使用 ' 定义字符串
如果字符串内部需要使用 ',可以使用 " 定义字符串
可以使用 索引 获取一个字符串中 指定位置的字符,索引计数从 0 开始
也可以使用 for 循环遍历 字符串中每一个字符
大多数编程语言都是用 "
来定义字符串
string = "Hello Python"
for c in string:
print(c)

1.2 字符串的常用操作
- 在
ipython3
中定义一个 字符串,例如:hello_str = ""
- 输入
hello_str.
按下 TAB
键,ipython
会提示 字符串 能够使用的 方法 如下:
In [1]: hello_str.
hello_str.capitalize hello_str.isidentifier hello_str.rindex
hello_str.casefold hello_str.islower hello_str.rjust
hello_str.center hello_str.isnumeric hello_str.rpartition
hello_str.count hello_str.isprintable hello_str.rsplit
hello_str.encode hello_str.isspace hello_str.rstrip
hello_str.endswith hello_str.istitle hello_str.split
hello_str.expandtabs hello_str.isupper hello_str.splitlines
hello_str.find hello_str.join hello_str.startswith
hello_str.format hello_str.ljust hello_str.strip
hello_str.format_map hello_str.lower hello_str.swapcase
hello_str.index hello_str.lstrip hello_str.title
hello_str.isalnum hello_str.maketrans hello_str.translate
hello_str.isalpha hello_str.partition hello_str.upper
hello_str.isdecimal hello_str.replace hello_str.zfill
hello_str.isdigit hello_str.rfind
提示:正是因为 python 内置提供的方法足够多,才使得在开发时,能够针对字符串进行更加灵活的操作!应对更多的开发需求!
1) 判断类型 - 9

2) 查找和替换 - 7

hello_str = "hello world"
# 1. 判断是否以指定字符串开始,False
print(hello_str.startswith("Hello"))
# 2. 判断是否以指定字符串结束,True
print(hello_str.endswith("world"))
# 3. 查找指定字符串
# index同样可以查找指定的字符串在大字符串中的索引
print(hello_str.find("llo")) # 2
# index如果指定的字符串不存在,会报错
# find如果指定的字符串不存在,会返回-1
print(hello_str.find("abc"))
# 4. 替换字符串
# replace方法执行完成之后,会返回一个新的字符串
# 注意:不会修改原有字符串的内容
print(hello_str.replace("world", "python")) # hello python
print(hello_str) # hello world
3) 大小写转换 - 5

4) 文本对齐 - 3

5) 去除空白字符 - 3

# 假设:以下内容是从网络上抓取的
# 要求:顺序并且居中对齐输出以下内容
poem = ["\t\n登鹳雀楼",
"王之涣",
"白日依山尽\t\n",
"黄河入海流",
"欲穷千里目",
"更上一层楼"]
for poem_str in poem:
# 先使用strip方法去除字符串中的空白字符
# 再使用center方法居中显示文本
print("|%s|" % poem_str.strip().center(10, " "))
6) 拆分和连接 - 5

# 假设:以下内容是从网络上抓取的
# 要求:
# 1. 将字符串中的空白字符全部去掉
# 2. 再使用 " " 作为分隔符,拼接成一个整齐的字符串
poem_str = "登鹳雀楼\t 王之涣 \t 白日依山尽 \t \n 黄河入海流 \t\t 欲穷千里目 \t\t\n更上一层楼"
print(poem_str)
# 1. 拆分字符串
poem_list = poem_str.split()
print(poem_list)
# 2. 合并字符串
result = " ".join(poem_list)
print(result)

1.3 字符串的切片
- 切片 使用 索引值 来限定范围,从一个大的 字符串 中 切出 小的 字符串
- 列表 和 元组 都是 有序 的集合,都能够 通过索引值 获取到对应的数据
- 字典 是一个 无序 的集合,是使用 键值对 保存数据
字符串[开始索引:结束索引:步长]
注意:
指定的区间属于 左闭右开 型 [开始索引, 结束索引) => 开始索引 >= 范围 < 结束索引
从 起始 位开始,到 结束位的前一位 结束(不包含结束位本身)
从头开始,开始索引 数字可以省略,冒号不能省略
到末尾结束,结束索引 数字可以省略,冒号不能省略
步长默认为 1,如果连续切片,数字和冒号都可以省略
索引的顺序和倒序
在 Python 中不仅支持 顺序索引,同时还支持 倒序索引
所谓倒序索引就是 从右向左 计算索引
最右边的索引值是 -1,依次递减
演练需求
截取从 2 ~ 5 位置 的字符串
截取从 2 ~ 末尾 的字符串
截取从 开始 ~ 5 位置 的字符串
截取完整的字符串
从开始位置,每隔一个字符截取字符串
从索引 1 开始,每隔一个取一个
截取从 2 ~ 末尾 - 1 的字符串
截取字符串末尾两个字符
字符串的逆序(面试题)
答案
num_str = "0123456789"
# 1. 截取从 2 ~ 5 位置 的字符串,"2345"
print(num_str[2:6])
# 2. 截取从 2 ~ `末尾` 的字符串,"23456789"
print(num_str[2:])
# 3. 截取从 `开始` ~ 5 位置 的字符串,"012345"
print(num_str[:6])
# 4. 截取完整的字符串,"0123456789"
print(num_str[:])
# 5. 从开始位置,每隔一个字符截取字符串,"02468"
print(num_str[::2])
# 6. 从索引 1 开始,每隔一个取一个,"13579"
print(num_str[1::2])
# 倒序切片
# -1 表示倒数第一个字符,"9"
print(num_str[-1])
# 7. 截取从 2 ~ `末尾 - 1` 的字符串,"2345678"
print(num_str[2:-1])
# 8. 截取字符串末尾两个字符,"89"
print(num_str[-2:])
# 9. 字符串的逆序(面试题),"9876543210"
print(num_str[::-1])