Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 and ASCII

I am learning python, and I am a bit confused about contents.encode() in init() in the following code.

PY3 = sys.version_info[0] > 2


class Test:
    def __init__(self):
        self.contents = ''
        if PY3:
            self.contents = self.contents.encode('ascii')

1 Answers

Python 3 strings are Unicode strings. There are situations where you want data in a byte string, where (typically) every character is a single byte. "string".encode('ascii') creates a byte string containing the six ASCII characters s, t, r, i, n, g out of the Unicode string containing these characters as Unicode.

This is a portability tweak; Python 2 strings were byte strings (though there is the u"string" notation for creating Unicode strings, starting in Python 2.5 IIRC).

For a richer exposition of the precise difference, perhaps see http://nedbatchelder.com/text/unipain.html

like image 186
tripleee Avatar answered Nov 06 '25 12:11

tripleee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!