Utf-8

2 snippets across 2 stacks - HTML & CSS, Python

HCHTML & CSS

Character Encoding

HC · Document Structure
Syntax
<meta charset="UTF-8">
Example
<head>
  <meta charset="UTF-8">
</head>

Note Must appear within the first 1024 bytes of the document. UTF-8 handles virtually all global characters and emoji.

PYPython

Encode & Decode

PY · Strings
Syntax
str.encode(encoding)
bytes.decode(encoding)
Example
text = "caf\u00e9"
encoded = text.encode("utf-8")
print(encoded)
print(encoded.decode("utf-8"))
Output
b'caf\xc3\xa9'
caf\u00e9

Note UTF-8 is the default encoding. Use errors='ignore' or errors='replace' to handle characters that cannot be encoded in the target encoding.

Frequently asked questions

How does HTML & CSS handle utf-8?
This task is covered in 2 stacks on this page: HTML & CSS, Python. The "Character Encoding" snippet in HTML & CSS uses `<meta charset="UTF-8">`.
Which code does the HTML & CSS example use?
The "Character Encoding" snippet uses `<meta charset="UTF-8">`, from the Document Structure section of the HTML & CSS cheat sheet.
Which stacks cover "utf-8" on this page?
HTML & CSS, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Character Encoding": Must appear within the first 1024 bytes of the document. UTF-8 handles virtually all global characters and emoji.