From d9f68235d0cee5788807f614361c8d0f02122cdc Mon Sep 17 00:00:00 2001 From: "rick.chan" Date: Mon, 23 Nov 2020 18:22:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E5=B9=B6=E8=A1=A5=E5=85=85=20r/b/u/f=20=E7=9A=84=E5=90=AB?= =?UTF-8?q?=E4=B9=89.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: rick.chan --- ...之间的转换.md => Python_string_与_bytes.md} | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) rename Software/Development/Language/Python/{Python_string_与_bytes_之间的转换.md => Python_string_与_bytes.md} (62%) diff --git a/Software/Development/Language/Python/Python_string_与_bytes_之间的转换.md b/Software/Development/Language/Python/Python_string_与_bytes.md similarity index 62% rename from Software/Development/Language/Python/Python_string_与_bytes_之间的转换.md rename to Software/Development/Language/Python/Python_string_与_bytes.md index 0ffa9c2..c5f322a 100644 --- a/Software/Development/Language/Python/Python_string_与_bytes_之间的转换.md +++ b/Software/Development/Language/Python/Python_string_与_bytes.md @@ -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 +```