# 格式化拼接(+, %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)
Python 格式化拼接、输出,编码解码,浅拷贝和深拷贝
最新推荐文章于 2024-09-29 14:13:49 发布