2、字符串
-
字符串中包含单引号或双引号
- 使用不同引号讲字符串括起来
- 对引号进行转义
str_1 = "hello' world" print(str_1) str_2 = 'hello\' world' print(str_2)
hello’ world
hello’ world -
字符串连接
# + 可作为字符串连接运算符 str_1 = 'hello' str_2 = ' world' print(str_1+str_2)
hello world
# 数值连接前,需要转换 iv_1 = 10 str_3 = str_1+str(iv_1) print(str_3) str_4 = str_1+repr(iv_1) print(str_4)
hello10
hello10说明:repr()会以Python表达式的形式来表示值。
print('hello') print(repr('hello'))
hello
‘hello’ -
input()生成提示,获取用户输入
str_1 = input('请输入:') print(str_1)
请输入:hello
hello -
长字符串
longstr_1 = '''hello world ! ''' print(longstr) longstr_2 = 'hello\nworld\n!' print(longstr_2)
hello
world
!hello
world
! -
原始字符串用 r 开头
rs_1 = r'hello\nworld\n!' print(rs_1)
hello\nworld\n!
rs_2 = r'hello\n\'world\n!' # 原始字符串中引号仍需转义 print(rs_2)
hello\n’world\n!
-
字节串 betys
-
将字符串转换成 bytes 对象:
- 如果字符串内容否是 ASCII 字符,可以通过直接在字符串之前添加 b 前缀来构建字节串值
- 调用 bytes() 函数(其实是 bytes 的构造方法)将字符串按指定字符串转成字节串
- 调用字符串本身的 encode() 方法将字符串按指定字符集转成字节串,默认使用UTF-8字符集
b_1 = b'abc' print(b_1) print(type(b_1))
b’abc’
<class ‘bytes’># 将字符串转换成字节串 b_2 = '你好'.encode() # 默认UTF-8 print(b_2) print(type(b_2)) b_3 = bytes('你好', 'UTF-8') print(b_3) print(type(b_3))
b’\xe4\xbd\xa0\xe5\xa5\xbd’
<class ‘bytes’>
b’\xe4\xbd\xa0\xe5\xa5\xbd’
<class ‘bytes’>说明:
- \x 表示十六进制的数,一位十六进制的数等于四位二进制的数,即两位十六进制的数等于 8 位二进制的数,得两位十六进制的数占一个字节(8 位二进制数占一个字节)。
- 一个中文字符在 UTF-8 中占 3 个字节
-
调用 bytes 对象的 decode() 方法将 bytes 对象解码成字符串
# 将字节串转换成字符串 b_4 = b'\xe4\xbd\xa0\xe5\xa5\xbd' print(b_4.decode('UTF-8'))
你好
-
-
转义字符
转义字符 说明 \b 退格符 \n 换行符 \r 回车符 \t 制表符 \" 双引号 \’ 单引号 \\ 反斜线 s_1 = 'a\tb\tc' print(s_1) s_2 = 'a\nb\nc' print(s_2)
a b c
a
b
c -
字符串格式化:使用 % 加转换说明的方式执行字符串格式化
转换说明符 说明 d, i 转换为带符号的十进制整数 o 转换为带符号的八进制整数 x, X 转换为带符号的十六进制整数 e, E 转换为科学计算法表示的浮点数 f, F 转换为十进制的浮点数 g 智能选择使用 f 或 e 格式 G 智能选择使用 F 或 E 格式 C 转换为单字符(只接受整数或单字符字符串) r 使用 repr() 将变量或表达式转换成字符串 s 使用 str() 将变量或表达式转换成字符串 s_1 = '我爱%s' print(s_1 % '你') s_2 = '我最爱的图书是%s,价格是%d' print(s_2 % ('疯狂Python讲义', 128)) price = 128 s_3 = '价格是%d, 八进制为%o, 十六进制为%x, 字符串形式为:%r' print(s_3 % (price, price, price, price))
我爱你
我最爱的图书是疯狂Python讲义,价格是128
价格是128, 八进制为200, 十六进制为80, 字符串形式为:128num = int(input('请输入一个整数:')) print('十六进制:%x' % num) print('八进制:%o' % num) # print('二进制:%b' % num) 字符串格式化不支持二进制输出,报错
请输入一个整数:30
十六进制:1e
八进制:36 -
调用函数(方法)操作字符串
-
函数
s_1 = 'abcdefgh' print(s_1[3]) # 根据下标访问 print(s_1[2:4]) # 指定开始、结束 print(s_1[2:6:2]) # 指定开始、结束、间隔(步长) print('abc' in s_1) # 是否包含 print('xyz' in s_1) print(len(s_1)) # 长度 print(max(s_1)) # 最大 print(min(s_1)) # 最小
d
cd
ce
True
False
8
h
a -
方法
print(s_1.upper()) # 全部大写 print(s_1.lower()) # 全部小写 print(s_1.title()) # 首字母大写
ABCDEFGH
abcdefgh
Abcdefghprint(' abc'.strip()) # 删除字符串前后的空白 print(' abc'.lstrip()) # 删除字符串前面(左边)的空白 print(' abc'.rstrip()) # 删除字符串后面(右边)的空白
abc
abc
abc其他方法 释义 startswith() 判断字符串是否以指定子串开头 endswith() 判断字符串是否以指定子串结尾 find() 查找指定子串在字符串中的出现位置,如果没有找到,则返回 -1 index() 查找指定子串在字符串中出现的位置,如果没有找到,则引发 ValueError 错误 replace() 使用指定子串替换字符串中的目标子串 translate() 使用指定的翻译映射表对字符串执行替换 split() 将字符串按指定分隔符分割成多个短语 join() 将多个短语连缀成字符串 print(s_1.find('def')) print(s_1.find('xyz'))
3
-1s_2 = 'abc.edf.gh' print(s_2.split('.'))
[‘abc’, ‘edf’, ‘gh’]
print('='.join(s_2.split('.')))
abc=edf=gh
-