Python3 and ASCII
12,392
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
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
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')