rexwashburn
1/25/2019 - 10:26 AM

String <--> Bytes

#!/usr/local/bin/python3.4
# coding:utf-8
import sys
 
sys.stdin =  open(sys.stdin.fileno(),  'r', encoding='UTF-8')
sys.stdout = open(sys.stdout.fileno(), 'w', encoding='UTF-8')
sys.stderr = open(sys.stderr.fileno(), 'w', encoding='UTF-8')
 
print("Content-Type: application/json; charset=UTF-8\r\n")

# String --> Bytes
str = 'あいうえお'
bytes = str.encode('utf-8')
print(bytes)
# --> b'\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86'

# Bytes --> String
text = bytes.decode('utf-8')
print(text)
# --> あいうえお