Reference library

Strings & text

Format, split, join, parse, and clean text — everyday Python string patterns.

1 match
Strings & text easy

How to Encode and Decode UTF-8 in Python

Convert a Python string to UTF-8 bytes with .encode() and back to text with .decode(), with a simple demo function.

utf-8 encode decode
Python
def encode_decode_demo(text: str):
    encoded = text.encode("utf-8")
    decoded = encoded.decode("utf-8")
    print(f"Original string: {text}")
    print(f"Encoded bytes: {encoded}")
    print(f"Decoded string: {decoded}")
    print(f"Match: {text == decoded}")

if __name__ == "__main__":
    encode_decode_demo("Hel…
14 0 Open

Browse by section

Each section groups closely related Python snippets.

Strings & text — Python code examples

What you will find here

This page collects strings & text snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.