字符串格式化
百分号格式化
需要指定数据类型,且需要一一对应。
s = "vae"
d = 32
print("我叫%s,年龄%d" % (s, d)) # 我叫 vae,年龄 32
Format 方法
Format 是 Python 特有的,不需要指定数据类型,不需要一一对应。
s = "vae"
d = 32
print("我叫{},年龄{}".format(s, d)) # 我叫 vae,年龄 32
print("我叫{0},年龄{1},真名{0}".format(s, d)) # 我叫 vae,年龄 32,真名 vae
L = ["许嵩", 32, "vae"]
print("我叫{0},年龄{1},真名{0}".format(*L)) # 我叫许嵩,年龄 32,真名许嵩
字符串编码(encode、decode)
Python 3 里面字符编码实际类型: a. 内存中字符运行的时候编码是 unicode,用空间换时间 b. 硬盘存储中或者网络传输过程中用 utf-8,追求空间
1 字节==8 位 utf-8:英文:8;中文:24; unicode:所有的都是 16 位
encode 编码:unicode 转换为指定编码 decode 解码:将原编码转换成 unicode
s = "中" # 程序执行时,"中"会以 unicode 形式存在在内存中
# encode 编码
s1 = s.encode("utf-8") # b'\xe4\xb8\xad'
print(s1)
s2 = s.encode("gbk") # b'\xd6\xd0'
print(s2)
# decode 解码
print(s1.decode("utf-8")) # 中
print(s2.decode("gbk")) # 中
# gbk-->utf-8 通过 unicode 来转换
print(s2.decode("gbk").encode("utf-8")) # b'\xe4\xb8\xad'
深浅拷贝
通过 id() 查看内存地址
s = "嵩"
n = 5
print(id(s)) # 2966737504576
print(id(n)) # 140713250513872
赋值
注: 字符串、数字:赋值还有深浅拷贝无意义,因为其永远指向同一个内存地址
n1 = {"k1":"v1", "k2":123, "k3":["hh", 18]}
n2 = n1
print(id(n1)) # 2568221657344
print(id(n2)) # 2568221657344
浅拷贝
只在内存中额外创建了第一层数据。
import copy
n1 = {"k1":"v1", "k2":123, "k3":["hh", 18]}
n3 = copy.copy(n1) # 浅拷贝,只在内存中额外创建了第一层数据
print(id(n1)) # 1907149736192
print(id(n3)) # 1907149736264 内存地址变化了
print(id(n1["k3"])) # 1907152948360
print(id(n3["k3"])) # 1907152948360 内存地址没有改变
深拷贝
只有最底层的内存地址没有改变。
import copy
n1 = {"k1":"v1", "k2":123, "k3":["hh", 18]}
n4 = copy.deepcopy(n1) # 深拷贝
print(id(n1)) # 2603325884672
print(id(n4)) # 2603328856856 内存地址变化了
print(id(n1["k3"])) # 2603329101384
print(id(n4["k3"])) # 2603329101384 内存地址改变了
print(id(n1["k3"][0])) # 3055213298104
print(id(n4["k3"][0])) # 3055213298104 内存地址没有改变
Bytes 与 Bytearray
- bytes
print(type("vae")) # <class 'str'>
print(type(b"vae")) # <class 'bytes'>
bytes 和 byte 的区别,字符串是字符的序列
- str 转换为 bytes
s = "中" # str
s1 = s.encode("utf-8") # 编码方式变成了 utf-8,str 通过 encode 转换为 bytes
print(s1) # b'\xe4\xb8\xad'
- bytes 通过 decode 转换为 str
s2 = s1.decode("utf-8") # decode 中传入其原来的编码方式:utf-8;系统自动将原来的编码方式 utf-8 解码为 unicode 编码方式
print(s2) # 中
- bytearray:可变
S1 = "你好,世界"
print(S1.encode("utf-8")) # bytes
B1 = bytearray(S1.encode("utf-8")) # bytearray
print(B1)
print(type(B1)) # <class 'bytearray'>
# 分解步骤
S = "哈哈"
S1 = "你好,世界"
B1 = bytearray(S1.encode("utf-8"))
B2 = bytearray(S.encode("utf-8"))
print(B2)
B1[:6] = B2 # 将 你好 替换成 哈哈
print(B1.decode("utf-8")) # 哈哈,世界
S1 = "你好,世界"
B1 = bytearray(S1.encode("utf-8"))
B1[:6] = bytearray("美丽", encoding="utf-8")
print(B1.decode("utf-8")) # 美丽,世界
