Best Way to Convert String to Bytes in Python 3

Best way to convert string to bytes in Python 3?

If you look at the docs for bytes, it points you to bytearray:

bytearray([source[, encoding[, errors]]])

Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Byte Array Methods.

The optional source parameter can be used to initialize the array in a few different ways:

If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

If it is an integer, the array will have that size and will be initialized with null bytes.

If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.

If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

Without an argument, an array of size 0 is created.

So bytes can do much more than just encode a string. It's Pythonic that it would allow you to call the constructor with any type of source parameter that makes sense.

For encoding a string, I think that some_string.encode(encoding) is more Pythonic than using the constructor, because it is the most self documenting -- "take this string and encode it with this encoding" is clearer than bytes(some_string, encoding) -- there is no explicit verb when you use the constructor.

I checked the Python source. If you pass a unicode string to bytes using CPython, it calls PyUnicode_AsEncodedString, which is the implementation of encode; so you're just skipping a level of indirection if you call encode yourself.

Also, see Serdalis' comment -- unicode_string.encode(encoding) is also more Pythonic because its inverse is byte_string.decode(encoding) and symmetry is nice.

How to convert a string to bytes exactly?

a = r'\x8e'
exec('my_string = b"' + a + '"')
print(my_string)

That does it.

is there a good way to convert python bytes to a string and back?

bytes.decode takes two parameters, really: the bytes to decode and (optionally) the scheme used to decode them. While you can write bytes.decode(b'...'), usually you write b'...'.decode(). Your zip file, though, isn't just an encoded Unicode string. If you want to "decode" them, use the zipfile module to decompress/unpack the files that are encoded as a stream of bytes.

Convert input () to bytes in Python 3?

You need to encode you message like this:

msg = input().encode()

The reason you did not need to do this in Python 2 is because unicode strings were then their own type, but in Python 3 all strings are now unicode by default.

Convert string of byte array to byte array in python

You can use ast.literal_eval to revert the string to bytes. (Thank you at Matiiss and Mark Tolonen for pointing out the problems with the eval method)

from ast import literal_eval

data = literal_eval(data_string)


Related Topics



Leave a reply



Submit