text.encoding
base64, base32, base58, URL and hex encodings, UTF-8 bytes, hex dumps
Example results generated on 2026-10-08. Ones using
now,todayor randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.
conversions
# to base64
"hi" to base64
# → "aGk="
Functions
| function | description |
|---|---|
base64(s: str) | base64-encode a string; same as s to base64 |
encode(s: str, fmt: str) | encode as “base64”, “base32”, “base58”, “url” or “hex” |
decode(s: str, fmt: str) | decode “base64”, “base32”, “base58”, “url” or “hex”; bytes that aren’t UTF-8 come back as a list |
ord(c: str) | Unicode code point of a single character |
chr(n: int) | character for a Unicode code point |
bytes(s: str) | list of the string’s UTF-8 bytes |
from_bytes(xs: list) | string from a list of UTF-8 bytes |
byte_len(v: str|list) | length in UTF-8 bytes rather than characters |
hexdump(v: str|list) | xxd-style dump of a string’s UTF-8 bytes or a list of bytes |
entropy(v: str|list) | Shannon entropy in bits per byte: 0 is constant, 8 is random |
base64
base64(s: str): base64-encode a string; same as s to base64
base64("hi there")
# → "aGkgdGhlcmU="
"hi" to base64
# → "aGk="
encode
encode(s: str, fmt: str): encode as “base64”, “base32”, “base58”, “url” or “hex”
"hi there".encode("base64")
# → "aGkgdGhlcmU="
"a b&c".encode("url")
# → "a%20b%26c"
"hi".encode("base32")
# → "NBUQ===="
See also: decode
decode
decode(s: str, fmt: str): decode “base64”, “base32”, “base58”, “url” or “hex”; bytes that aren’t UTF-8 come back as a list
"aGk=".decode("base64")
# → "hi"
"6869".decode("hex")
# → "hi"
"Cn8eVZg".decode("base58")
# → "hello"
See also: encode
ord
ord(c: str): Unicode code point of a single character
"A".ord
# → 65
"A".ord to hex
# → 0x41
chr
chr(n: int): character for a Unicode code point
97.chr
# → "a"
(65..70).map(chr).join
# → "ABCDE"
See also: ord
bytes
bytes(s: str): list of the string’s UTF-8 bytes
"hé".bytes
# → [104, 195, 169]
See also: from_bytes, ord
from_bytes
from_bytes(xs: list): string from a list of UTF-8 bytes
[104, 105].from_bytes
# → "hi"
byte_len
byte_len(v: str|list): length in UTF-8 bytes rather than characters
"héllo".byte_len
# → 6
hexdump
hexdump(v: str|list): xxd-style dump of a string’s UTF-8 bytes or a list of bytes
hexdump("hi\n")
# → "00000000: 6869 0a hi."
hexdump([0, 255, 65])
# → "00000000: 00ff 41 ..A"
entropy
entropy(v: str|list): Shannon entropy in bits per byte: 0 is constant, 8 is random
"aaaa".entropy
# → 0
"abcd".entropy
# → 2
See also: hexdump
More examples
encoding
# base64
"hi" to base64
# → "aGk="
# URL encoding
"a b&c".encode("url")
# → "a%20b%26c"
# base58, as in Bitcoin
"hello".encode("base58")
# → "Cn8eVZg"
# UTF-8 bytes
"héllo".bytes
# → [104, 195, 169, 108, 108, 111]
# code point to char
chr(9731)
# → "☃"
# bytes vs characters
["😀".byte_len, "😀".len]
# → [4, 1]
# look inside a string
hexdump("héllo\tworld\n")
# → "00000000: 68c3 a96c 6c6f 0977 6f72 6c64 0a h..llo.world."
# how random is it?
"aaaaaaaa".entropy
# → 0