Skip to content

Commit 42b44a7

Browse files
authored
[Term Entry] Python binascii-module: .a2b_qp()
* [Term Entry] Python binascii-module: .a2b_qp() * changed the codebyte example as both were identical * Minor changes ---------
1 parent 4908b6d commit 42b44a7

File tree

1 file changed

+70
-0
lines changed
  • content/python/concepts/binascii-module/terms/a2b-qp

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
Title: '.a2b_qp()'
3+
Description: 'Decodes a string of quoted-printable data back to binary.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Encoding'
9+
- 'Functions'
10+
CatalogContent:
11+
- 'learn-python-3'
12+
- 'paths/computer-science'
13+
---
14+
15+
In Python, the **`.a2b_qp()`** [function](https://www.codecademy.com/resources/docs/python/functions) decodes a string of quoted-printable data back to binary.
16+
17+
Quoted-printable is an encoding scheme used in email messages to represent non-ASCII characters using only ASCII characters.
18+
19+
## Syntax
20+
21+
```pseudo
22+
binascii.a2b_qp(data, header=False)
23+
```
24+
25+
**Parameters:**
26+
27+
- `data`: A bytes-like object containing the quoted-printable encoded data to decode.
28+
- `header`: When set to `True`, it decodes the data as if it were in an email header, handling special rules for headers. Default is `False`.
29+
30+
**Return value:**
31+
32+
The `.a2b_qp()` function returns the decoded binary data as a bytes object.
33+
34+
## Example
35+
36+
This example decodes a quoted-printable encoded byte string into its original binary form:
37+
38+
```py
39+
import binascii
40+
41+
# Quoted-printable encoded data
42+
encoded_data = b'Codecademy=20Docs'
43+
44+
# Decode the data back to binary
45+
decoded_data = binascii.a2b_qp(encoded_data)
46+
47+
print(decoded_data)
48+
```
49+
50+
This produces the following output:
51+
52+
```shell
53+
b'Codecademy Docs'
54+
```
55+
56+
## Codebyte Example
57+
58+
This example decodes a quoted-printable encoded message body retrieved from an email:
59+
60+
```codebyte/python
61+
import binascii
62+
63+
# Quoted-printable encoded email message
64+
email_body = b'Hello=2C=20thank=20you=20for=20joining=20Codecademy=21'
65+
66+
# Decode the email message body
67+
decoded_body = binascii.a2b_qp(email_body)
68+
69+
print(decoded_body)
70+
```

0 commit comments

Comments
 (0)