data and byte

numbers and bytes

a = 0b101010                    # binary number
b = b'123'                      # byte string
c = 0x4A6                       # hex number
d = 0o752                       # octal number

print()
print(f'a type is {type(a)}')
print(f'{hex(a)}      (includes 0x)')
print(':'.join(hex(ord(x))[2:] for x in hex(a)))

print()
print(f'b type is {type(b)}')
xx = b.hex()
print(f'{xx}  (type={type(xx)})')
print(':'.join(hex(ord(x))[2:] for x in xx))
for i in range(len(xx)):
    print(f'[{i}]  {xx[i]}  (ord={ord(xx[i])})')  

print()
print(f'c type is {type(c)}')
xx = hex(c)
print(f'{xx}  (type={type(xx)}      (includes 0x)')
print(':'.join(hex(ord(x))[2:] for x in hex(c)))

print()
print(f'd type is {type(d)}')
xx = hex(d)
print(f'{xx}  (type={type(xx)})      (includes 0x)')
print(':'.join(hex(ord(x))[2:] for x in hex(d)))

string as hex bytes

':'.join(hex(ord(x))[2:] for x in 'Hello, World!')

string to list of characters

def string_to_list(string):
   return list(string)

string = 'Now is the hour of our discontent'
print()
print(string)
print()
print(string_to_list(string))
print()
for i,c in enumerate(string_to_list(string)):
    print(f'[{i:2}]  {c}')

unicode

List of Unicode Characters

ustr = 'hello unicode \u00a9'
print()
print(ustr)
print(f'ustr type is {type(ustr)}')
print(f'character_count = {len(ustr)}')
print(f"byte_count = {len(ustr.encode('utf-8'))}")
print(':'.join(hex(ord(x))[2:] for x in ustr))