字符串format()格式化
a ='名字是:{0},年龄是:{1}‘ a.format('搞起',18) '名字是:搞起,年龄是:18' a.format('高希希',6) '名字是:高希希,年龄是:6' b='名字是:{0},年龄是:{1},{0}是个好小伙,{2}是个男孩' b.format('到期',18,'他') '名字是:到期,年龄是:18,到期是个好小伙,他是个男孩' c ='名字是:{name},年龄是:{age}' c.format(age=20,name='搞起') '名字是:搞起,年龄是:20'
format()填充与对齐:
{:*>8}'.format('234') #^ < >分别是居中,左对齐,右对齐,后代宽度。:号后面带填充的字符,只能是一个字符,不指定默认为空格 '*****234'
字符串的可变
import io s='hello,sxt' sio=io.StringIO(s) sio <_io.StringIO object at 0x00000213A7994940> sio.getvalue() 'hello,sxt' sio <_io.StringIO object at 0x00000213A7994940> s 'hello,sxt' sio.seek(7) 7 sio.write('g') 1 sio.getvalue() 'hello,sgt' s 'hello,sxt' sio.getvalue() 'hello,sgt'
bin()可以将数字转成二进制