Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use quopri.encode() in Python

Ayesha Naeem

The quopri.encode() function in Python performs quoted-printable encoding on data with few non-printable characters. quopri.encode() converts data with majority US ASCII characters to data with all US ASCII characters.

To use the quopri.encode() function, the program needs to import the quopri module as shown below:

import quopri

Prototype and parameters

quopri.encode() accepts input, output, and quotetabs as non-optional parameters.

  • The input and output parameters accept binary file objects. The quopri.encode()function encodes the input binary file and writes the quoted-printable encoded data to the output file.
  • The quotestab parameter accepts a True or False value. The quopri.encode() function encodes spaces and tabs if the quotetabs is true; otherwise, it leaves them unencoded.

Note: Tabs and spaces at the end of a line are always encoded.

  • The header parameter accepts a True or False value. quopri.encode() encodes spaces as underscores if the header is true; otherwise, it leaves them unencoded.

Note: quopri.encode() uses False as the default value of header.


The quopri.encode() function encodes all 8-bit value characters into a = and two hexadecimal digits except printable ASCII characters. For example, = has a decimal value of 61 and is encoded as =3D.

All ASCII characters with decimal values from 33 to 126 are printable and decoded as well as encoded as themselves. An exception is = with an ASCII value of 61.

A space with ASCII value 9 and a tab with ASCII value 32 is encoded as themselves.


The following code demonstrates the use of the quopri.encode() function:

import quopri
from io import BytesIO 

#Example 1

#example text
text = 'This text contains ünicöde'

#create an output file
outputFile = BytesIO()

#create an input file
inputFile= BytesIO((text).encode('utf-8'))

quopri.encode(inputFile, outputFile, quotetabs = False)

#get content of the encoded file

#Example 2

inputFile2= BytesIO((text).encode('utf-8'))
outputFile2 = BytesIO()
quopri.encode(inputFile2, outputFile2, quotetabs = True)


#Example 3

inputFile3= BytesIO((text).encode('utf-8'))
outputFile3 = BytesIO()
quopri.encode(inputFile3, outputFile3, quotetabs = False, header = True)


The example text contains two non-printable characters and three spaces.

The program creates input and output files using the BytesIO module. BytesIO creates an in-memory binary file-like object. Once created, the program can read from and write to it like a regular file.

The program displays the output through the getValue() function, which extracts the file’s contents.

Example 1

The quopri.encode() function takes the quotetabs parameter as False and displays the spaces as themselves as a result. The program encodes Unicode characters ü and ö as =C3=BC and =C3=B6 respectively.

Note: utf-8 encoding of Unicode characters can take up to four bytes.

Example 2

The quopri.encode() function takes the quotetabs parameter as False and encodes the spaces as =20 as a result.

Example 3

The quopri.encode() function takes the header parameter as True and encodes the spaces as _ as a result.




Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring