字符串和编码
开篇先说前几天看过的《编码的奥秘》
通过灯泡开关,摩斯代码,十进制,八进制,四进制,讲到了二进制的含义。
思路全都通畅了。
在计算机内存中,统一用Unicode编码,当需要保存到硬盘或需要传输的时候,就转换成UTF-8(8-bit Unicode Transformation Format)
Python的字符串
在Python3中,字符串是以Unicode编码的,也就是说Python的字符串,支持多种语言。
print('包启中文的str')

ord()函数获取字符的整数表示,chr()函数把编码转换为对应的字符

str通过encode()方法可以编码为指定的bytes,
把bytes变为str,就需要用decode()

不在编码范围内就会报错。
len()函数计算的是str的字符数,如果换成bytes,len()函数就计算字节数:

%运算符就是用来格式化字符串的。在字符串内部,%s表示用字符串替换,%d表示用整数替换,有几个%?占位符,后面就跟几个变量或者值,顺序要对应好,如果只有一个%?,括号可以省略。

格式化整数和浮点安徽还可以指定是否补0和整体与小数的位数:


网友评论