From 6c55685981c00ad06f47da573d453b7ef6732ea1 Mon Sep 17 00:00:00 2001 From: Lonercode Date: Wed, 29 Oct 2025 16:27:01 +0100 Subject: [PATCH 1/4] add code for bytes to strings article --- python-bytes-to-strings/decode_bytes1.py | 8 ++++++++ python-bytes-to-strings/decode_bytes2.py | 12 ++++++++++++ python-bytes-to-strings/raw_bytes.py | 1 + python-bytes-to-strings/replace_option_example.py | 2 ++ python-bytes-to-strings/test.py | 4 ++++ python-bytes-to-strings/unicode_decode_error.py | 2 ++ 6 files changed, 29 insertions(+) create mode 100644 python-bytes-to-strings/decode_bytes1.py create mode 100644 python-bytes-to-strings/decode_bytes2.py create mode 100644 python-bytes-to-strings/raw_bytes.py create mode 100644 python-bytes-to-strings/replace_option_example.py create mode 100644 python-bytes-to-strings/test.py create mode 100644 python-bytes-to-strings/unicode_decode_error.py diff --git a/python-bytes-to-strings/decode_bytes1.py b/python-bytes-to-strings/decode_bytes1.py new file mode 100644 index 0000000000..0d57760478 --- /dev/null +++ b/python-bytes-to-strings/decode_bytes1.py @@ -0,0 +1,8 @@ +from urllib.request import urlopen + +url = "https://example.com/" + +with urlopen(url) as response: + raw_bytes: bytes = response.read() + +print(f"Bytes: {raw_bytes[:100]}\n") diff --git a/python-bytes-to-strings/decode_bytes2.py b/python-bytes-to-strings/decode_bytes2.py new file mode 100644 index 0000000000..0825f26261 --- /dev/null +++ b/python-bytes-to-strings/decode_bytes2.py @@ -0,0 +1,12 @@ +from urllib.request import urlopen + +url = "https://example.com/" + +with urlopen(url) as response: + raw_bytes: bytes = response.read() + +print(f"Bytes: {raw_bytes[:100]}\n") + +string_format = raw_bytes[:100].decode() + +print(f"String format: {string_format}\n") diff --git a/python-bytes-to-strings/raw_bytes.py b/python-bytes-to-strings/raw_bytes.py new file mode 100644 index 0000000000..effd3cc6e9 --- /dev/null +++ b/python-bytes-to-strings/raw_bytes.py @@ -0,0 +1 @@ +raw_bytes = b"These are some interesting bytes" diff --git a/python-bytes-to-strings/replace_option_example.py b/python-bytes-to-strings/replace_option_example.py new file mode 100644 index 0000000000..a765d41247 --- /dev/null +++ b/python-bytes-to-strings/replace_option_example.py @@ -0,0 +1,2 @@ +hex_representation = b"\xff\xfe\xfa" +print(hex_representation.decode("utf-8", errors="replace")) diff --git a/python-bytes-to-strings/test.py b/python-bytes-to-strings/test.py new file mode 100644 index 0000000000..26b969af8f --- /dev/null +++ b/python-bytes-to-strings/test.py @@ -0,0 +1,4 @@ +raw_bytes = b"These are some interesting bytes" +raw_bytes.replace("y", "o") + +print(raw_bytes) diff --git a/python-bytes-to-strings/unicode_decode_error.py b/python-bytes-to-strings/unicode_decode_error.py new file mode 100644 index 0000000000..46ad504363 --- /dev/null +++ b/python-bytes-to-strings/unicode_decode_error.py @@ -0,0 +1,2 @@ +hex_representation = b"\xff\xfe\xfa" +print(hex_representation.decode("utf-8")) From 0be976e384c4e048bf5165ed261255dc9fb12bb7 Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 29 Oct 2025 16:52:33 +0100 Subject: [PATCH 2/4] Fix indentation --- python-bytes-to-strings/decode_bytes1.py | 2 +- python-bytes-to-strings/decode_bytes2.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python-bytes-to-strings/decode_bytes1.py b/python-bytes-to-strings/decode_bytes1.py index 0d57760478..9594a1372f 100644 --- a/python-bytes-to-strings/decode_bytes1.py +++ b/python-bytes-to-strings/decode_bytes1.py @@ -3,6 +3,6 @@ url = "https://example.com/" with urlopen(url) as response: - raw_bytes: bytes = response.read() + raw_bytes: bytes = response.read() print(f"Bytes: {raw_bytes[:100]}\n") diff --git a/python-bytes-to-strings/decode_bytes2.py b/python-bytes-to-strings/decode_bytes2.py index 0825f26261..cb6e485e87 100644 --- a/python-bytes-to-strings/decode_bytes2.py +++ b/python-bytes-to-strings/decode_bytes2.py @@ -3,7 +3,7 @@ url = "https://example.com/" with urlopen(url) as response: - raw_bytes: bytes = response.read() + raw_bytes: bytes = response.read() print(f"Bytes: {raw_bytes[:100]}\n") From e2c8871cba1fc2af7b5196d6fa4a9a7fc9c81c91 Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 29 Oct 2025 16:56:37 +0100 Subject: [PATCH 3/4] Add README --- python-bytes-to-strings/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 python-bytes-to-strings/README.md diff --git a/python-bytes-to-strings/README.md b/python-bytes-to-strings/README.md new file mode 100644 index 0000000000..0a7808592d --- /dev/null +++ b/python-bytes-to-strings/README.md @@ -0,0 +1,3 @@ +# How to Convert Bytes to Strings in Python + +The materials contained in this folder are designed to complement the Real Python tutorial [How to Convert Bytes to Strings in Python](https://realpython.com/convert-python-bytes-to-strings/). From 77c9819ae8b854739ad369aa22b6ecd77330a405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Zaczy=C5=84ski?= Date: Sun, 16 Nov 2025 13:29:31 +0100 Subject: [PATCH 4/4] Final QA --- python-bytes-to-strings/decode_bytes1.py | 2 +- python-bytes-to-strings/decode_bytes2.py | 7 ++----- python-bytes-to-strings/error_handling.py | 6 ++++++ python-bytes-to-strings/{test.py => mixup.py} | 2 -- python-bytes-to-strings/raw_bytes.py | 1 - python-bytes-to-strings/replace_option_example.py | 2 -- python-bytes-to-strings/unicode_decode_error.py | 2 -- 7 files changed, 9 insertions(+), 13 deletions(-) create mode 100644 python-bytes-to-strings/error_handling.py rename python-bytes-to-strings/{test.py => mixup.py} (80%) delete mode 100644 python-bytes-to-strings/raw_bytes.py delete mode 100644 python-bytes-to-strings/replace_option_example.py delete mode 100644 python-bytes-to-strings/unicode_decode_error.py diff --git a/python-bytes-to-strings/decode_bytes1.py b/python-bytes-to-strings/decode_bytes1.py index 9594a1372f..b3a66f28f0 100644 --- a/python-bytes-to-strings/decode_bytes1.py +++ b/python-bytes-to-strings/decode_bytes1.py @@ -5,4 +5,4 @@ with urlopen(url) as response: raw_bytes: bytes = response.read() -print(f"Bytes: {raw_bytes[:100]}\n") +print("Bytes:", raw_bytes[:100]) diff --git a/python-bytes-to-strings/decode_bytes2.py b/python-bytes-to-strings/decode_bytes2.py index cb6e485e87..09c668a647 100644 --- a/python-bytes-to-strings/decode_bytes2.py +++ b/python-bytes-to-strings/decode_bytes2.py @@ -5,8 +5,5 @@ with urlopen(url) as response: raw_bytes: bytes = response.read() -print(f"Bytes: {raw_bytes[:100]}\n") - -string_format = raw_bytes[:100].decode() - -print(f"String format: {string_format}\n") +print("Bytes:", raw_bytes[:100]) +print("String:", raw_bytes[:100].decode()) diff --git a/python-bytes-to-strings/error_handling.py b/python-bytes-to-strings/error_handling.py new file mode 100644 index 0000000000..fd776c5ee1 --- /dev/null +++ b/python-bytes-to-strings/error_handling.py @@ -0,0 +1,6 @@ +encoded_text = b"d\xe9j\xe0 vu" + +print(f"{encoded_text.decode("utf-8", errors="ignore") = }") +print(f"{encoded_text.decode("utf-8", errors="replace") = }") +print(f"{encoded_text.decode("utf-8", errors="backslashreplace") = }") +print(f"{encoded_text.decode('utf-8')}") diff --git a/python-bytes-to-strings/test.py b/python-bytes-to-strings/mixup.py similarity index 80% rename from python-bytes-to-strings/test.py rename to python-bytes-to-strings/mixup.py index 26b969af8f..8ce0167e20 100644 --- a/python-bytes-to-strings/test.py +++ b/python-bytes-to-strings/mixup.py @@ -1,4 +1,2 @@ raw_bytes = b"These are some interesting bytes" raw_bytes.replace("y", "o") - -print(raw_bytes) diff --git a/python-bytes-to-strings/raw_bytes.py b/python-bytes-to-strings/raw_bytes.py deleted file mode 100644 index effd3cc6e9..0000000000 --- a/python-bytes-to-strings/raw_bytes.py +++ /dev/null @@ -1 +0,0 @@ -raw_bytes = b"These are some interesting bytes" diff --git a/python-bytes-to-strings/replace_option_example.py b/python-bytes-to-strings/replace_option_example.py deleted file mode 100644 index a765d41247..0000000000 --- a/python-bytes-to-strings/replace_option_example.py +++ /dev/null @@ -1,2 +0,0 @@ -hex_representation = b"\xff\xfe\xfa" -print(hex_representation.decode("utf-8", errors="replace")) diff --git a/python-bytes-to-strings/unicode_decode_error.py b/python-bytes-to-strings/unicode_decode_error.py deleted file mode 100644 index 46ad504363..0000000000 --- a/python-bytes-to-strings/unicode_decode_error.py +++ /dev/null @@ -1,2 +0,0 @@ -hex_representation = b"\xff\xfe\xfa" -print(hex_representation.decode("utf-8"))