Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-bytes-to-strings/README.md
Original file line number Diff line number Diff line change
@@ -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/).
8 changes: 8 additions & 0 deletions python-bytes-to-strings/decode_bytes1.py
Original file line number Diff line number Diff line change
@@ -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")
12 changes: 12 additions & 0 deletions python-bytes-to-strings/decode_bytes2.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions python-bytes-to-strings/raw_bytes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
raw_bytes = b"These are some interesting bytes"
2 changes: 2 additions & 0 deletions python-bytes-to-strings/replace_option_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hex_representation = b"\xff\xfe\xfa"
print(hex_representation.decode("utf-8", errors="replace"))
4 changes: 4 additions & 0 deletions python-bytes-to-strings/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
raw_bytes = b"These are some interesting bytes"
raw_bytes.replace("y", "o")

print(raw_bytes)
2 changes: 2 additions & 0 deletions python-bytes-to-strings/unicode_decode_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hex_representation = b"\xff\xfe\xfa"
print(hex_representation.decode("utf-8"))