In Python, there is an encode
method in unicode strings to encode from unicode to byte string. There is a decode
method in string to do the reverse.
But I'm confused what the encode
method in string for?
Python String encode() converts a string value into a collection of bytes, using an encoding scheme specified by the user.
Python String encode() MethodThe encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.
What does encode() do in Python? In today's world, security is critical in many applications. As a result, secure information storage in the database is required, and encoded copies of strings must be saved. To achieve this encoding process, Python encode() function is used.
UTF-8 is a byte oriented encoding. The encoding specifies that each character is represented by a specific sequence of one or more bytes.
It's useful for non-text codecs.
>>> 'Hello, world!'.encode('hex')
'48656c6c6f2c20776f726c6421'
>>> 'Hello, world!'.encode('base64')
'SGVsbG8sIHdvcmxkIQ==\n'
>>> 'Hello, world!'.encode('zlib')
'x\x9c\xf3H\xcd\xc9\xc9\xd7Q(\xcf/\xcaIQ\x04\x00 ^\x04\x8a'
It first decodes to Unicode using the default encoding, then encodes back to a byte string.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> sys.setdefaultencoding('latin-1')
>>> '\xc4'.encode('utf-8')
'\xc3\x84'
Here, '\xc4'
is Latin-1 for Ä and '\xc3\x84'
is UTF-8 for Ä.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With