Python String Encoding, Decoding, and ASCII Conversion: A Practical Guide
This note demonstrates how Python encodes and decodes Unicode strings using utf-8 and GBK, and how the chr() and ord() functions convert between characters and their ASCII codes, with concrete code examples and output illustrations.
The article explains Python's string encoding and decoding mechanisms, showing that the encode() method converts a Unicode string such as "我爱你中国" into a bytes object. By default it uses UTF‑8, but explicit encodings like 'utf-8' and 'gbk' can be specified.
# Encoding and decoding
str1 = "我爱你中国"
print(str1.encode()) # b'\xe6\x88\x91\xe7\x88\xb1\xe4\xba\xad\zb8\xad\xe5\x9b\xbd'
print(str1.encode('utf-8'))
print(str1.encode('gbk'))The resulting byte strings are displayed, illustrating the different byte sequences produced by UTF‑8 and GBK encodings.
The note then covers ASCII code conversion in Python. The chr() function transforms an integer ASCII value into its corresponding character, while ord() returns the integer value of a given character.
print(chr(97)) # a
print(chr(68)) # D
print(ord("c")) # 99
print(ord("z")) # 90The output confirms that chr(97) yields "a", chr(68) yields "D", and that ord() correctly retrieves the ASCII codes 99 and 90 for "c" and "z" respectively.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
