TIL

Today I Learned. 知ったこと、学んだことを書いていく

strとbytes

Python3ではstrとUTF-8 bytesの相互変換が簡単にできる

  • strからbytesに変換:encode("utf-8")
  • bytesからstrに変換:decode("utf-8")

strからbytes

>>> test1 = "あいう".encode("utf-8")
>>> test1
b'\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86'

bytesからstr

>>> test1.decode("utf-8")
'あいう'

参考文献

python3のbytes型とstr型の比較と変換方法 | Python Snippets