Convert string to binary in python

I am in need of a way to get the binary representation of a string in python. e.g.

    st = "hello world"
    toBinary(st)

Is there a module of some neat way of doing this?

Something like this?

    >>> st = "hello world"
    >>> ' '.join(format(ord(x), 'b') for x in st)
    '1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'

    #using `bytearray`
    >>> ' '.join(format(x, 'b') for x in bytearray(st))
    '1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'

From: stackoverflow.com/q/18815820

Back to homepage or read more recommendations: