Remember
encode() converts a string into bytes using an encoding. UTF-8 can use several bytes for one character.
encode() converts a string into bytes using an encoding. UTF-8 can use several bytes for one character.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'café'
result = text.encode("utf-8")
print(result)Top-level scope
'café'Object 2 · bytesb'caf\xc3\xa9'encode() converts a string into bytes using an encoding. UTF-8 can use several bytes for one character.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'café'
result = text.encode("utf-8")
print(result)
|
v
DISPLAYED RESULT
b'caf\xc3\xa9'
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (bytes)
object 1: 'café'
object 2: b'caf\xc3\xa9'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'café'
result = text.encode("utf-8")
print(result)Top-level scope
'café'Object 2 · bytesb'caf\xc3\xa9'The example has finished. Compare the final output with the values and references above.
b'caf\xc3\xa9'
What do you expect this program to print?
text = 'café'
result = text.encode("utf-8")
print(result)string.encode() MethodThe encode() method in Python is used to convert a string into a bytes object using a specified encoding format (default is UTF-8). This is useful when you need to transmit or store text in a binary format.
string.encode(encoding="utf-8", errors="strict")
encoding – (Optional) The encoding format. Default is "utf-8".errors – (Optional) Defines how to handle encoding errors. Common values:
"strict" – Raise a UnicodeEncodeError (default)"ignore" – Ignore characters that can’t be encoded"replace" – Replace with a question mark (?)bytes object representing the encoded string.text = "Hello World"
encoded_text = text.encode()
print(encoded_text)
b'Hello World'
text = "Café"
encoded_text = text.encode("latin-1")
print(encoded_text)
b'Café'
text = "你好"
encoded_text = text.encode("ascii", errors="replace")
print(encoded_text)
b'??'
encode() on a bytes object instead of a string"uft-8" instead of "utf-8")If you're dealing with file I/O or web scraping, understanding how string.encode() and bytes.decode() work is essential.
string.encode() converts a string to bytes.utf-8, ascii, latin-1.strict, ignore, replace.Write a program that takes a string input from the user and prints its byte representation using UTF-8 encoding.
user_input = input("Enter a string: ")
print("Encoded:", user_input.encode("utf-8"))
Connect what you noticed in the example with the idea below.
encode() converts a string into bytes using an encoding. UTF-8 can use several bytes for one character.
encode() always returns another str.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.