python
1、字符串的拼接和格式化
a = 'ssd ’
a * 3
'ssd ssd ssd ’
‘’.join(str1, str2, str3) 可以做字符串的拼接
a = [‘a’, ‘b’, ‘c’]
‘’.join(a)
‘abc’
’ %s %s %s '%(str1,str2,str3) 格式化字符串 %s是占位符
‘hello %s’ % ‘word’
‘hello word’
使用format
‘{} {} {}’.format(obj1,obj2,obj3)
‘{} {} {}’.format(‘a’, ‘b’, ‘c’)
‘a b c’
‘{0} {1} {2}’.format(obj1,obj2,obj3)
‘{0} {1} {2}’.format(‘a’, ‘b’, ‘c’)
‘a b c’
‘{1} {0} {2}’.format(obj1,obj2,obj3)
‘{1} {0} {2}’.format(‘a’, ‘b’, ‘c’)
‘b a c’
字符格式化
format 了解
‘{:.2f}’.format(12.333) 保留小数点后两位
‘{:.2f}’.format(12.33333)
‘12.33’‘{a:.2f}’.format(a=12.33333)
‘12.33’
‘{:.2%}’.format(0.333) 百分比格式
‘{:.2%}’.format(0.333)
‘33.30%’
‘{:x}’.format(20) 转换成十六进制
‘{:x}’.format(20)
‘14’
‘{😮}’.format(20) 八进制
‘{😮}’.format(20)
‘24’‘{:2}’.format(20)
‘20’
2、深浅复制 浅复制第一层不会受到原来列表的影响,内层会被影响
深复制都不会受影响
li = [1, 2, 3]
l2 = [‘a’, li]
浅复制:
l3 = l2.copy()
l3 = l2[:]
a = [1, 2, 3]
list_b = [1, a]
list_c =list_b.copy()
list_c
[1, [1, 2, 3]]list_b
[1, [1, 2, 3]]list_b[1][0] = 100
list_b
[1, [100, 2, 3]]list_c
[1, [100, 2, 3]]
深复制
import copy
l4 = copy.deepcopy(l2)
import copy
list_b = [1, [1, 2, 3]]
list_c = copy.deepcopy(list_b)
list_c
[1, [1, 2, 3]]
3、bytes和bytearray(二进制数组)
bytes(10)
b’\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00’
bytes(b’abc’)
b’abc’
bytearray(10)
bytearray(b’\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00’)