NotePublic/Software/Development/Language/Python/Python_str_与_bytes_之间的转换.md

23 lines
328 B
Markdown
Raw Normal View History

# 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)
```