Python String Encoding and Base64 Operations

String Representation in Python

Python 3 uses Unicode encoding in memory to support multilingual characters. Hexadecimal Unicode sequences are equivalent to string literals:


>>> '\u0048\u0065\u006c\u006c\u006f'
'Hello'

Character Ecnoding Functions

The ord() function returns a character's integer representation, while chr() converts an integer back to a character:


>>> ord('X')
88
>>> ord('€')
8364
>>> chr(122)
'z'
>>> chr(9731)
'☃'

Bytes Data Type

Strings must be converted to bytes for storage or transmission. Bytes objects use single-byte representation:


text = 'Python'
str_data = text
byte_data = b'Python'

>>> len("您好")
2
>>> len("您好".encode('utf-8'))
6

Base64 Encoding Implementation

Base64 proivdes reversible encoding for non-criticla data, producing ASCII characters with possible padding equals signs:


import base64

original = "Data1234"
encoded = base64.b64encode(original.encode('utf-8'))

>>> print(encoded)
b'RGF0YTEyMzQ='
>>> decoded = base64.b64decode(encoded).decode('utf-8')
>>> print(decoded)
Data1234

Tags: python unicode bytes Base64 encoding

Posted on Sun, 04 Oct 2026 16:52:26 +0000 by mikosiko