Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python String encode method

Tags:

python

unicode

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?

like image 985
amit Avatar asked Mar 03 '11 06:03

amit


People also ask

What is Python string encode?

Python String encode() converts a string value into a collection of bytes, using an encoding scheme specified by the user.

How do I set the encoding of a string in Python?

Python String encode() MethodThe encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.

Why do we use encode () in Python?

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.

What does encoding =' UTF-8 do in Python?

UTF-8 is a byte oriented encoding. The encoding specifies that each character is represented by a specific sequence of one or more bytes.


2 Answers

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'
like image 160
Ignacio Vazquez-Abrams Avatar answered Oct 06 '22 12:10

Ignacio Vazquez-Abrams


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 Ä.

like image 21
Petri Lehtinen Avatar answered Oct 06 '22 12:10

Petri Lehtinen