0
0
PythonProgramBeginner · 2 min read

Python Program to Encode and Decode String

Use encoded = original_string.encode('utf-8') to encode a string to bytes and decoded = encoded.decode('utf-8') to decode bytes back to a string in Python.
📋

Examples

Inputhello
OutputEncoded: b'hello', Decoded: hello
InputPython 3.10
OutputEncoded: b'Python 3.10', Decoded: Python 3.10
Input
OutputEncoded: b'', Decoded:
🧠

How to Think About It

To encode a string means to convert it into bytes so computers can store or send it easily. Decoding reverses this, turning bytes back into a readable string. We use encode() to convert string to bytes and decode() to convert bytes back to string, specifying the same character encoding like 'utf-8' for both.
📐

Algorithm

1
Get the input string from the user or program.
2
Convert the string to bytes using <code>encode()</code> with 'utf-8' encoding.
3
Print or store the encoded bytes.
4
Convert the bytes back to string using <code>decode()</code> with 'utf-8' encoding.
5
Print or return the decoded string.
💻

Code

python
original_string = 'hello world'
encoded = original_string.encode('utf-8')
print('Encoded:', encoded)
decoded = encoded.decode('utf-8')
print('Decoded:', decoded)
Output
Encoded: b'hello world' Decoded: hello world
🔍

Dry Run

Let's trace the string 'hello world' through the encoding and decoding steps.

1

Original string

original_string = 'hello world'

2

Encode string

encoded = b'hello world' (bytes representation)

3

Print encoded

Output: Encoded: b'hello world'

4

Decode bytes

decoded = 'hello world' (string)

5

Print decoded

Output: Decoded: hello world

StepVariableValue
1original_string'hello world'
2encodedb'hello world'
4decoded'hello world'
💡

Why This Works

Step 1: Encoding the string

The encode() method converts the string into bytes using 'utf-8', which computers can handle easily.

Step 2: Printing encoded bytes

The encoded bytes show with a leading b' to indicate they are byte data, not a normal string.

Step 3: Decoding back to string

The decode() method converts the bytes back to the original string using the same 'utf-8' encoding.

🔄

Alternative Approaches

Base64 Encoding
python
import base64
original = 'hello world'
encoded = base64.b64encode(original.encode('utf-8'))
print('Encoded:', encoded)
decoded = base64.b64decode(encoded).decode('utf-8')
print('Decoded:', decoded)
Base64 encodes bytes into ASCII characters, useful for safe transmission but increases size.
Using codecs module
python
import codecs
original = 'hello world'
encoded = codecs.encode(original, 'utf-8')
print('Encoded:', encoded)
decoded = codecs.decode(encoded, 'utf-8')
print('Decoded:', decoded)
The codecs module provides similar encode/decode but is less commonly used for utf-8.

Complexity: O(n) time, O(n) space

Time Complexity

Encoding and decoding process each character once, so time grows linearly with string length.

Space Complexity

Both encoded bytes and decoded string require space proportional to input size.

Which Approach is Fastest?

Direct encode() and decode() are fastest and simplest; Base64 adds overhead for safe transmission.

ApproachTimeSpaceBest For
utf-8 encode/decodeO(n)O(n)Simple string conversion
Base64 encodingO(n)O(n)Safe transmission over text-only channels
codecs moduleO(n)O(n)Alternative encoding methods
💡
Always use the same encoding (like 'utf-8') for both encoding and decoding to avoid errors.
⚠️
Trying to decode a string that was never encoded to bytes causes errors.