企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 开头r b u f 含义 * `b` bytes字节符,打印以b开头。 ``` b'input\n' 输出: b'input\n' ``` * `r `非转义原生字符,经处理'\n'变成了'\\'和'n'。也就是\n表示的是两个字符,而不是换行。 ``` r'input\n' # 输出:'input\\n' ``` * `u` unicode编码字符,python3默认字符串编码方式。 ``` u'input\n' 输出:'input\n' ``` * ` f `以f开头表示在字符串内支持大括号内的python 表达式 ``` import time t0 = time.time() time.sleep(1) name = 'processing' print(f'{name} done in {time.time() - t0:.2f} s') 输出:processing done in 1.00 s ``` * str与bytes转换 ``` str = "€20" print(str.encode("utf-8")) print(str.encode("utf-8").decode("utf-8")) ``` ``` s1 = '123' print(s1) print(type(s1)) s2 = b'123' print(s2) print(type(s2)) 区别输出: 123 <class 'str'> b'123' <class 'bytes'> ``` * Python 3 将字符串处理为 unicode 类型。 ``` str转bytes: bytes('123', encoding='utf8') str.encode('123') bytes转str: str(b'123', encoding='utf-8') bytes.decode(b'123') ``` python3 文件默认编码是utf-8 , 字符串编码是 unicode 以utf-8 或者 gbk等编码的代码,加载到内存,会自动转为unicode正常显示。 ![](https://box.kancloud.cn/7b9068c459645423041f33b0e7a40e94_276x176.png) [ Python常见字符编码及其之间的转换](https://blog.csdn.net/sinat_36972314/article/details/79745438)