---
title: 4.27 Encoding and Decoding
description: "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…"
url: "https://magenta-tarsier-454184.hostingersite.com/docs/python-programming/strings-python/4-27-encoding-and-decoding/"
updated: "2026-07-29"
category: Python Programming / Strings
---

# 4.27 Encoding and Decoding

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 `b` indicates 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'xe0xa4xa8xe0xa4xaexe0xa4xb8xe0xa5x8dxe0xa4xa4xe0xa5x87'
```

### 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'xf0x9fx98x8a'
```

### 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 xe0xa4xa8xe0xa4xaexe0xa4xb8xe0xa5x8dxe0xa4xa4xe0xa5x87'
```

### 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 of `str`.
- `decode()` is a method of `bytes`.
- UTF-8 is the default and most widely used encoding.
- `encode()` returns a value of type `bytes`.
- `decode()` returns a value of type `str`.

**Congratulations!** You have now completed **Chapter 4: Strings**. The next chapter will introduce **Lists**, Python's first built-in mutable collection type.
