Files
team-learning-program/PythonLanguage/08. 字符串.md
T
2020-07-31 17:39:54 +08:00

453 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 字符串
# 字符串
## 1. 字符串的定义
- Python 中字符串被定义为引号之间的字符集合。
- Python 支持使用成对的 单引号 或 双引号。
【例子】
```python
t1 = 'i love Python!'
print(t1, type(t1))
# i love Python! <class 'str'>
t2 = "I love Python!"
print(t2, type(t2))
# I love Python! <class 'str'>
print(5 + 8) # 13
print('5' + '8') # 58
```
- Python 的常用转义字符
转义字符 | 描述
:---:|---
`\\` | 反斜杠符号
`\'` | 单引号
`\"` | 双引号
`\n` | 换行
`\t` | 横向制表符(TAB)
`\r` | 回车
【例子】如果字符串中需要出现单引号或双引号,可以使用转义符号`\`对字符串中的符号进行转义。
```python
print('let\'s go') # let's go
print("let's go") # let's go
print('C:\\now') # C:\now
print("C:\\Program Files\\Intel\\Wifi\\Help")
# C:\Program Files\Intel\Wifi\Help
```
【例子】原始字符串只需要在字符串前边加一个英文字母 r 即可。
```python
print(r'C:\Program Files\Intel\Wifi\Help')
# C:\Program Files\Intel\Wifi\Help
```
【例子】三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。
```python
para_str = """这是一个多行字符串的实例
多行字符串可以使用制表符
TAB ( \t )。
也可以使用换行符 [ \n ]。
"""
print(para_str)
# 这是一个多行字符串的实例
# 多行字符串可以使用制表符
# TAB ( )。
# 也可以使用换行符 [
# ]。
para_str = '''这是一个多行字符串的实例
多行字符串可以使用制表符
TAB ( \t )。
也可以使用换行符 [ \n ]。
'''
print(para_str)
# 这是一个多行字符串的实例
# 多行字符串可以使用制表符
# TAB ( )。
# 也可以使用换行符 [
# ]。
```
## 2. 字符串的切片与拼接
- 类似于元组具有不可修改性
- 从 0 开始 (和 Java 一样)
- 切片通常写成 `start:end` 这种形式,包括「`start` 索引」对应的元素,不包括「`end`索引」对应的元素。
- 索引值可正可负,正索引从 0 开始,从左往右;负索引从 -1 开始,从右往左。使用负数索引时,会从最后一个元素开始计数。最后一个元素的位置编号是 -1。
【例子】
```python
str1 = 'I Love LsgoGroup'
print(str1[:6]) # I Love
print(str1[5]) # e
print(str1[:6] + " 插入的字符串 " + str1[6:])
# I Love 插入的字符串 LsgoGroup
s = 'Python'
print(s) # Python
print(s[2:4]) # th
print(s[-5:-2]) # yth
print(s[2]) # t
print(s[-1]) # n
```
## 3. 字符串的常用内置方法
- `capitalize()` 将字符串的第一个字符转换为大写。
【例子】
```python
str2 = 'xiaoxie'
print(str2.capitalize()) # Xiaoxie
```
- `lower()` 转换字符串中所有大写字符为小写。
- `upper()` 转换字符串中的小写字母为大写。
- `swapcase()` 将字符串中大写转换为小写,小写转换为大写。
【例子】
```python
str2 = "DAXIExiaoxie"
print(str2.lower()) # daxiexiaoxie
print(str2.upper()) # DAXIEXIAOXIE
print(str2.swapcase()) # daxieXIAOXIE
```
- `count(str, beg= 0,end=len(string))` 返回`str`在 string 里面出现的次数,如果`beg`或者`end`指定则返回指定范围内`str`出现的次数。
【例子】
```python
str2 = "DAXIExiaoxie"
print(str2.count('xi')) # 2
```
- `endswith(suffix, beg=0, end=len(string))` 检查字符串是否以指定子字符串 `suffix` 结束,如果是,返回 True,否则返回 False。如果 `beg``end` 指定值,则在指定范围内检查。
- `startswith(substr, beg=0,end=len(string))` 检查字符串是否以指定子字符串 `substr` 开头,如果是,返回 True,否则返回 False。如果 `beg``end` 指定值,则在指定范围内检查。
【例子】
```python
str2 = "DAXIExiaoxie"
print(str2.endswith('ie')) # True
print(str2.endswith('xi')) # False
print(str2.startswith('Da')) # False
print(str2.startswith('DA')) # True
```
- `find(str, beg=0, end=len(string))` 检测 `str` 是否包含在字符串中,如果指定范围 `beg``end`,则检查是否包含在指定范围内,如果包含,返回开始的索引值,否则返回 -1。
- `rfind(str, beg=0,end=len(string))` 类似于 `find()` 函数,不过是从右边开始查找。
【例子】
```python
str2 = "DAXIExiaoxie"
print(str2.find('xi')) # 5
print(str2.find('ix')) # -1
print(str2.rfind('xi')) # 9
```
- `isnumeric()` 如果字符串中只包含数字字符,则返回 True,否则返回 False。
【例子】
```python
str3 = '12345'
print(str3.isnumeric()) # True
str3 += 'a'
print(str3.isnumeric()) # False
```
- `ljust(width[, fillchar])`返回一个原字符串左对齐,并使用`fillchar`(默认空格)填充至长度`width`的新字符串。
- `rjust(width[, fillchar])`返回一个原字符串右对齐,并使用`fillchar`(默认空格)填充至长度`width`的新字符串。
【例子】
```python
str4 = '1101'
print(str4.ljust(8, '0')) # 11010000
print(str4.rjust(8, '0')) # 00001101
```
- `lstrip([chars])` 截掉字符串左边的空格或指定字符。
- `rstrip([chars])` 删除字符串末尾的空格或指定字符。
- `strip([chars])` 在字符串上执行`lstrip()``rstrip()`
【例子】
```python
str5 = ' I Love LsgoGroup '
print(str5.lstrip()) # 'I Love LsgoGroup '
print(str5.lstrip().strip('I')) # ' Love LsgoGroup '
print(str5.rstrip()) # ' I Love LsgoGroup'
print(str5.strip()) # 'I Love LsgoGroup'
print(str5.strip().strip('p')) # 'I Love LsgoGrou'
```
- `partition(sub)` 找到子字符串sub,把字符串分为一个三元组`(pre_sub,sub,fol_sub)`,如果字符串中不包含sub则返回`('原字符串','','')`
- `rpartition(sub)`类似于`partition()`方法,不过是从右边开始查找。
【例子】
```python
str5 = ' I Love LsgoGroup '
print(str5.strip().partition('o')) # ('I L', 'o', 've LsgoGroup')
print(str5.strip().partition('m')) # ('I Love LsgoGroup', '', '')
print(str5.strip().rpartition('o')) # ('I Love LsgoGr', 'o', 'up')
```
- `replace(old, new [, max])` 把 将字符串中的`old`替换成`new`,如果`max`指定,则替换不超过`max`次。
【例子】
```python
str5 = ' I Love LsgoGroup '
print(str5.strip().replace('I', 'We')) # We Love LsgoGroup
```
- `split(str="", num)` 不带参数默认是以空格为分隔符切片字符串,如果`num`参数有设置,则仅分隔`num`个子字符串,返回切片后的子字符串拼接的列表。
【例子】
```python
str5 = ' I Love LsgoGroup '
print(str5.strip().split()) # ['I', 'Love', 'LsgoGroup']
print(str5.strip().split('o')) # ['I L', 've Lsg', 'Gr', 'up']
```
【例子】
```python
u = "www.baidu.com.cn"
# 使用默认分隔符
print(u.split()) # ['www.baidu.com.cn']
# 以"."为分隔符
print((u.split('.'))) # ['www', 'baidu', 'com', 'cn']
# 分割0次
print((u.split(".", 0))) # ['www.baidu.com.cn']
# 分割一次
print((u.split(".", 1))) # ['www', 'baidu.com.cn']
# 分割两次
print(u.split(".", 2)) # ['www', 'baidu', 'com.cn']
# 分割两次,并取序列为1的项
print((u.split(".", 2)[1])) # baidu
# 分割两次,并把分割后的三个部分保存到三个变量
u1, u2, u3 = u.split(".", 2)
print(u1) # www
print(u2) # baidu
print(u3) # com.cn
```
【例子】去掉换行符
```python
c = '''say
hello
baby'''
print(c)
# say
# hello
# baby
print(c.split('\n')) # ['say', 'hello', 'baby']
```
【例子】
```python
string = "hello boy<[www.baidu.com]>byebye"
print(string.split('[')[1].split(']')[0]) # www.baidu.com
print(string.split('[')[1].split(']')[0].split('.')) # ['www', 'baidu', 'com']
```
- `splitlines([keepends])` 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数`keepends`为 False,不包含换行符,如果为 True,则保留换行符。
【例子】
```python
str6 = 'I \n Love \n LsgoGroup'
print(str6.splitlines()) # ['I ', ' Love ', ' LsgoGroup']
print(str6.splitlines(True)) # ['I \n', ' Love \n', ' LsgoGroup']
```
- `maketrans(intab, outtab)` 创建字符映射的转换表,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
- `translate(table, deletechars="")` 根据参数`table`给出的表,转换字符串的字符,要过滤掉的字符放到`deletechars`参数中。
【例子】
```python
str7 = 'this is string example....wow!!!'
intab = 'aeiou'
outtab = '12345'
trantab = str7.maketrans(intab, outtab)
print(trantab) # {97: 49, 111: 52, 117: 53, 101: 50, 105: 51}
print(str7.translate(trantab)) # th3s 3s str3ng 2x1mpl2....w4w!!!
```
## 4. 字符串格式化
- `format` 格式化函数
【例子】
```python
str8 = "{0} Love {1}".format('I', 'Lsgogroup') # 位置参数
print(str8) # I Love Lsgogroup
str8 = "{a} Love {b}".format(a='I', b='Lsgogroup') # 关键字参数
print(str8) # I Love Lsgogroup
str8 = "{0} Love {b}".format('I', b='Lsgogroup') # 位置参数要在关键字参数之前
print(str8) # I Love Lsgogroup
str8 = '{0:.2f}{1}'.format(27.658, 'GB') # 保留小数点后两位
print(str8) # 27.66GB
```
- Python 字符串格式化符号
符 号 | 描述
:---:|:---
%c | 格式化字符及其ASCII码
%s | 格式化字符串,用str()方法处理对象
%r | 格式化字符串,用rper()方法处理对象
%d | 格式化整数
%o | 格式化无符号八进制数
%x | 格式化无符号十六进制数
%X | 格式化无符号十六进制数(大写)
%f | 格式化浮点数字,可指定小数点后的精度
%e | 用科学计数法格式化浮点数
%E | 作用同%e,用科学计数法格式化浮点数
%g | 根据值的大小决定使用%f或%e
%G | 作用同%g,根据值的大小决定使用%f或%E
【例子】
```python
print('%c' % 97) # a
print('%c %c %c' % (97, 98, 99)) # a b c
print('%d + %d = %d' % (4, 5, 9)) # 4 + 5 = 9
print("我叫 %s 今年 %d 岁!" % ('小明', 10)) # 我叫 小明 今年 10 岁!
print('%o' % 10) # 12
print('%x' % 10) # a
print('%X' % 10) # A
print('%f' % 27.658) # 27.658000
print('%e' % 27.658) # 2.765800e+01
print('%E' % 27.658) # 2.765800E+01
print('%g' % 27.658) # 27.658
text = "I am %d years old." % 22
print("I said: %s." % text) # I said: I am 22 years old..
print("I said: %r." % text) # I said: 'I am 22 years old.'
```
- 格式化操作符辅助指令
符号 | 功能
:---:|:---
`m.n` | m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话)
`-` | 用作左对齐
`+` | 在正数前面显示加号( + )
`#` | 在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X'(取决于用的是'x'还是'X')
`0` | 显示的数字前面填充'0'而不是默认的空格
【例子】
```python
print('%5.1f' % 27.658) # ' 27.7'
print('%.2e' % 27.658) # 2.77e+01
print('%10d' % 10) # ' 10'
print('%-10d' % 10) # '10 '
print('%+d' % 10) # +10
print('%#o' % 10) # 0o12
print('%#x' % 108) # 0x6c
print('%010d' % 5) # 0000000005
```
---
**参考文献**
- https://www.runoob.com/python3/python3-tutorial.html
- https://www.bilibili.com/video/av4050443
- https://mp.weixin.qq.com/s/DZ589xEbOQ2QLtiq8mP1qQ
---
**练习题**
1、字符串函数回顾
- 怎么批量替换字符串中的元素?
- 怎么把字符串按照空格进⾏拆分?
- 怎么去除字符串⾸位的空格?
2、实现isdigit函数
题目要求
实现函数isdigit, 判断字符串里是否只包含数字0~9
```python
def isdigit(string):
"""
判断字符串只包含数字
:param string:
:return:
"""
# your code here
pass
```
3、leetcode 5题 最长回文子串
给定一个字符串 `s`,找到 `s` 中最长的回文子串。你可以假设 `s` 的最大长度为 1000。
示例:
输入: "babad"
输出: "bab"
输入: "cbbd"
输出: "bb"
```python
class Solution:
def longestPalindrome(self, s: str) -> str:
# your code here
```