---
title: "encode and decode string function in python?"  
description: "encode and decode string function in python?"  
author: "ICSM Computer"  
published: 2025-09-17  
updated: 2025-09-17  
canonical: https://www.mindstick.com/interview/34374/encode-and-decode-string-function-in-python  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 2 minutes  

---

# encode and decode string function in python?

**Predefined (built-in / library) functions** in Python that handle **encoding** and **decoding**.

### 1. String encode() method

Converts a Python string (`str`) to bytes.

```python
text = "Hello नमस्ते"
encoded = text.encode("utf-8")   # str → bytes
print(encoded)   # b'Hello \xe0\xa4...'
```

### 2. Bytes decode() method

Converts bytes back to string.

```python
decoded = encoded.decode("utf-8")   # bytes → str
print(decoded)   # "Hello नमस्ते"
```

### 3. Base64 predefined functions (from `base64` module)

```python
import base64

text = "Python Encoding"

# Encode string to Base64
encoded = base64.b64encode(text.encode("utf-8"))
print(encoded)  # b'UHl0aG9uIEVuY29kaW5n'

# Decode Base64 back to string
decoded = base64.b64decode(encoded).decode("utf-8")
print(decoded)  # Python Encoding
```

So the **predefined functions** you’ll use most often are:

- `str.encode(encoding)`
- `bytes.decode(encoding)`
- `base64.b64encode()`
- `base64.b64decode()`

## Answers

### Answer by ICSM Computer

**Predefined (built-in / library) functions** in Python that handle **encoding** and **decoding**.

### 1. String encode() method

Converts a Python string (`str`) to bytes.

```python
text = "Hello नमस्ते"
encoded = text.encode("utf-8")   # str → bytes
print(encoded)   # b'Hello \xe0\xa4...'
```

### 2. Bytes decode() method

Converts bytes back to string.

```python
decoded = encoded.decode("utf-8")   # bytes → str
print(decoded)   # "Hello नमस्ते"
```

### 3. Base64 predefined functions (from `base64` module)

```python
import base64

text = "Python Encoding"

# Encode string to Base64
encoded = base64.b64encode(text.encode("utf-8"))
print(encoded)  # b'UHl0aG9uIEVuY29kaW5n'

# Decode Base64 back to string
decoded = base64.b64decode(encoded).decode("utf-8")
print(decoded)  # Python Encoding
```

So the **predefined functions** you’ll use most often are:

- `str.encode(encoding)`
- `bytes.decode(encoding)`
- `base64.b64encode()`
- `base64.b64decode()`


---

Original Source: https://www.mindstick.com/interview/34374/encode-and-decode-string-function-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
