Skip to content

Instantly share code, notes, and snippets.

@lelandbatey
Last active May 11, 2023 16:51
Show Gist options
  • Save lelandbatey/795de5768a5b8dd430318d9ac088ff81 to your computer and use it in GitHub Desktop.
Save lelandbatey/795de5768a5b8dd430318d9ac088ff81 to your computer and use it in GitHub Desktop.
Pretty print large base 10 numbers and base 2 numbers
# Take a very large number and pretty print it in triplets of 3 digits, each triplet separated by a space.
def pnum_spc(n): print(' '.join([''.join(list(str(n))[::-1][i:i+3][::-1]) for i in range(0, len(str(n)), 3)][::-1]))
# >>> pnum_spc(32 ** 13)
# 36 893 488 147 419 103 232
# Print numbers as 32-bit binary numbers w/ spaces giving 4-bit words
def pbin_spc(n): print(' '.join([''.join(list(f'{n:032b}')[::-1][i:i+4][::-1]) for i in range(0, len(f'{n:032b}'), 4)][::-1]))
# >>> "{0:032b}".format(1234)
# '00000000000000000000010011010010'
# >>> pbin_spc(1234)
# 0000 0000 0000 0000 0000 0100 1101 0010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment