A base64 string is a way of encoding binary data (like images, audio, or files) into a text format. This is useful for transmitting data over mediums that are designed to handle text (such as JSON or XML) or for embedding binary data directly into web pages.
Base64 represents binary data as a sequence of ASCII characters. It does this by dividing the binary data into chunks of 6 bits (since ASCII uses 64 printable characters) and then mapping those chunks to a predefined set of 64 ASCII characters.
For example:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA..."/>
{ "image": "iVBORw0KGgoAAAANSUhEUgAAAAUA..." }
import base64 # Encode a string to base64 data = "Hello, World!" encoded = base64.b64encode(data.encode()) print(encoded) # Output: b'SGVsbG8sIFdvcmxkIQ==' # Decode a base64 string decoded = base64.b64decode(encoded).decode() print(decoded) # Output: "Hello, World!"
The above is the detailed content of What is basestring in Python?. For more information, please follow other related articles on the PHP Chinese website!