- Syntax
- Example 1: Encoding a String
- Example 2: Encoding with UTF-8
- Example 3: Encoding Unicode Text
- Example 4: Decoding Bytes
- Example 5: User Input
- Example 6: Encoding and Decoding Together
- Example 7: Checking the Data Type After Encoding
- Example 8: Checking the Data Type After Decoding
- Example 9: Encoding an Emoji
- Example 10: Encoding Mixed Languages
- Common Mistakes
- Best Practices
- Key Points to Remember
Computers do not store characters directly. Instead, they store bytes, which are sequences of numbers. Encoding converts a string into bytes, while decoding converts bytes back into a string.
Python provides the encode() and decode() methods for converting between strings and bytes. The most commonly used encoding standard is UTF-8, which supports characters from almost every language.
Encoding and decoding are essential when reading and writing files, sending data over networks, communicating with APIs, and storing text in binary formats.
Syntax #
string.encode()
or
string.encode("utf-8")
or
bytes_object.decode()
or
bytes_object.decode("utf-8")
Components #
| Component | Description |
|---|---|
string |
The Unicode string to encode. |
encode() |
Converts a string into bytes. |
bytes_object |
The bytes object to decode. |
decode() |
Converts bytes back into a string. |
"utf-8" |
The encoding format used for conversion. |
Example 1: Encoding a String #
main.py
text = "Python"
print(text.encode())
Output #
b'Python'
Explanation #
encode()converts the string into bytes.- The prefix
bindicates a bytes object. - UTF-8 encoding is used by default.
Example 2: Encoding with UTF-8 #
main.py
text = "Hello"
print(text.encode("utf-8"))
Output #
b'Hello'
Explanation #
"utf-8"is specified explicitly.- The result is the same because UTF-8 is the default encoding.
- A bytes object is returned.
Example 3: Encoding Unicode Text #
main.py
text = "เคจเคฎเคธเฅเคคเฅ"
print(text.encode("utf-8"))
Output #
b'\xe0\xa4\xa8\xe0\xa4\xae\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4\xe0\xa5\x87'
Explanation #
- Unicode characters are converted into UTF-8 bytes.
- Each character may occupy multiple bytes.
- The result is a bytes object.
Example 4: Decoding Bytes #
main.py
data = b'Python'
print(data.decode())
Output #
Python
Explanation #
decode()converts bytes back into a string.- UTF-8 decoding is used by default.
- The returned value is a string.
Example 5: User Input #
main.py
text = input("Enter text: ")
encoded = text.encode()
print(encoded)
Sample Input #
Hello
Output #
b'Hello'
Explanation #
- The user enters a string.
encode()converts the string into bytes.- The encoded bytes are displayed.
Example 6: Encoding and Decoding Together #
main.py
text = "Python"
encoded = text.encode()
decoded = encoded.decode()
print(decoded)
Output #
Python
Explanation #
- The string is first encoded into bytes.
- The bytes are then decoded back into a string.
- The original text is restored.
Example 7: Checking the Data Type After Encoding #
main.py
text = "Python"
result = text.encode()
print(type(result))
Output #
<class 'bytes'>
Explanation #
encode()returns a bytes object.- The data type is
bytes. - It is different from
str.
Example 8: Checking the Data Type After Decoding #
main.py
data = b'Python'
result = data.decode()
print(type(result))
Output #
<class 'str'>
Explanation #
decode()converts bytes into a string.- The returned value is of type
str. - The original bytes remain unchanged.
Example 9: Encoding an Emoji #
main.py
text = "๐"
print(text.encode("utf-8"))
Output #
b'\xf0\x9f\x98\x8a'
Explanation #
- Emojis are Unicode characters.
- UTF-8 represents them using multiple bytes.
- The encoded value is displayed as hexadecimal escape sequences.
Example 10: Encoding Mixed Languages #
main.py
text = "Hello เคจเคฎเคธเฅเคคเฅ"
encoded = text.encode("utf-8")
print(encoded)
Output #
b'Hello \xe0\xa4\xa8\xe0\xa4\xae\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4\xe0\xa5\x87'
Explanation #
- The English and Hindi text are encoded together.
- UTF-8 supports characters from multiple languages.
- The result is a single bytes object.
Common Mistakes #
1. Calling decode() on a String #
Incorrect
text = "Python"
print(text.decode())
Output #
AttributeError: 'str' object has no attribute 'decode'
Reason
decode() is a method of bytes, not str.
Correct
data = b'Python'
print(data.decode())
2. Calling encode() on Bytes #
Incorrect
data = b'Python'
print(data.encode())
Output #
AttributeError: 'bytes' object has no attribute 'encode'
Reason
encode() is a method of strings, not bytes.
3. Assuming encode() Modifies the Original String #
Incorrect
text = "Python"
text.encode()
print(type(text))
Output #
<class 'str'>
Reason
encode() returns a new bytes object.
The original string remains unchanged.
4. Assuming Encoded Data Is Still a String #
Incorrect
text = "Python"
encoded = text.encode()
print(type(encoded))
Incorrect Expectation #
<class 'str'>
Actual Output #
<class 'bytes'>
Reason
Encoding converts a string into a bytes object.
Best Practices #
- Use UTF-8 for most encoding and decoding operations.
- Encode text before writing binary data or sending data over a network.
- Decode bytes immediately after reading binary text data.
- Remember that strings and bytes are different data types.
- Store encoded and decoded values in separate variables for better readability.
Key Points to Remember #
- Encoding converts a string into bytes.
- Decoding converts bytes back into a string.
encode()is a method ofstr.decode()is a method ofbytes.- UTF-8 is the default and most widely used encoding.
encode()returns a value of typebytes.decode()returns a value of typestr.
Congratulations! You have now completed Chapter 4: Strings. The next chapter will introduce Lists, Python’s first built-in mutable collection type.