Python 格式化拼接、输出,编码解码,浅拷贝和深拷贝

# 格式化拼接(+, %s, join, format, f)
a = "hello"
b = "world"
c = "!"
print(a + " " + b + " " + c)
print("%s %s %s" % (a, b, c))
# join 参数必须是序列类型(list tuple) 才能保证顺序
print(" ".join([a, b, c]))
print("{} {} {}".format(a, b, c))
print("{0} {1} {2}".format(a, b, c))
print("{0} {2} {1}".format(a, b, c))
print('{n1} {n2} {n3}'.format(n1=a, n2=b, n3=c))
print(f"{a} {b} {c}")
# join 参数如果是散列类型, 顺序是随机的
print(" ".join({"a", "b", "c"}))

# 格式化输出(%f) 浮点数 .3f 保留3位小数
d = 3.1415926
print("%.3f" % d)

# 编码解码
str = "你好"
b_str = str.encode("utf-8")
print(b_str)
print(b_str.decode("utf-8"))
print(str.encode("gbk"))
print(str.encode("gb18030"))

# copy浅拷贝(不会拷贝引用对象) deepcopy深拷贝(会拷贝引用对象)
import copy
e = [1, 2, 3, [4, 5, 6]]
f = e.copy()
# 修改会影响拷贝对象
e[3][0] = 100
print(e, f)
g = copy.deepcopy(e)
# 修改不会影响拷贝对象
e[3][0] = 200
print(e, g)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值