修改文件名并补充 r/b/u/f 的含义.
Signed-off-by: rick.chan <chenyang@autoai.com>
This commit is contained in:
parent
ba1355eb5f
commit
d9f68235d0
|
@ -1,4 +1,4 @@
|
|||
# Python string 与 bytes 之间的转换
|
||||
# Python string 与 bytes
|
||||
|
||||
总的来说,bytes 和 string 的关系是:
|
||||
|
||||
|
@ -40,3 +40,23 @@ s = str(s, encoding = "utf8") # string
|
|||
s = '\\u4eca\\u5929\\u5929\\u6c14\\u4e0d\\u9519' # 中文是:今天天气真不错
|
||||
new_s = s.encode().decode('unicode_escape') # 输出为:今天天气真不错
|
||||
```
|
||||
|
||||
## 4.string 开头 r/b/u/f 的含义
|
||||
|
||||
```python
|
||||
b'input\n' # bytes字节符,打印以b开头。输出:b'input\n'
|
||||
r'input\n' # 非转义原生字符,经处理'\n'变成了'\\'和'n'。也就是\n表示的是两个字符,而不是换行。输出:'input\\n'
|
||||
u'input\n' # unicode编码字符,python3默认字符串编码方式。输出:'input\n'
|
||||
```
|
||||
|
||||
f 开头:
|
||||
|
||||
```python
|
||||
import time
|
||||
t0 = time.time()
|
||||
time.sleep(1)
|
||||
name = 'processing'
|
||||
print(f'{name} done in {time.time() - t0:.2f} s') # 以f开头表示在字符串内支持大括号内的python 表达式
|
||||
输出:
|
||||
processing done in 1.00 s
|
||||
```
|
Loading…
Reference in New Issue