Created
November 16, 2018 02:26
-
-
Save rcx/a86703da453bbf3406f9b67c6e22edc4 to your computer and use it in GitHub Desktop.
python3 encoding cheatsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.6 | |
my_string = 'hello world' | |
# get bytes from string | |
my_bytes = my_string.encode() # default is utf8. accepts: utf-8, utf16, ascii, etc | |
print(my_bytes) | |
# get hex from bytes | |
my_hex = my_bytes.hex() # NEW in python3.5, on python<3.4 use binascii (un)hexlify | |
print(my_hex) | |
# get string from hex | |
my_string_again = bytes.fromhex(my_hex).decode() # default encoding is utf8 | |
print(my_string_again) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment