From 74c4ad25227e034068a0a36c259c9ac2630b9dab Mon Sep 17 00:00:00 2001 From: "rick.chan" Date: Tue, 13 Oct 2020 13:34:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Python=20str=20=E4=B8=8E?= =?UTF-8?q?=20bytes=20=E4=B9=8B=E9=97=B4=E7=9A=84=E8=BD=AC=E6=8D=A2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: rick.chan --- .../Python/Python_str_与_bytes_之间的转换.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Software/Development/Language/Python/Python_str_与_bytes_之间的转换.md diff --git a/Software/Development/Language/Python/Python_str_与_bytes_之间的转换.md b/Software/Development/Language/Python/Python_str_与_bytes_之间的转换.md new file mode 100644 index 0000000..093d1d9 --- /dev/null +++ b/Software/Development/Language/Python/Python_str_与_bytes_之间的转换.md @@ -0,0 +1,22 @@ +# Python str 与 bytes 之间的转换 + +```python +# bytes object +b = b"example" + +# str object +s = "example" + +# str to bytes +sb = bytes(s, encoding = "utf8") + +# bytes to str +bs = str(b, encoding = "utf8") + +# an alternative method +# str to bytes +sb2 = str.encode(s) + +# bytes to str +bs2 = bytes.decode(b) +```