Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use quopri.decodestring() in Python

Ayesha Naeem

The quopri.decodestring() function in Python performs quoted-printable decoding on data with few non-printable characters.

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

import quopri

Prototype and parameters

The function accepts a byte source input as a non-optional parameter, and returns the decoded bytes as the output.

The header parameter accepts a True or False value. The function decodes spaces as underscores if the header is True; otherwise, it leaves them unencoded.

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


The quopri.decodestring() function decodes all 8-bit value characters encoded into a = and two hexadecimal digits, with the exception of printable ASCII characters. For example, =3D has a decimal value of 61 and is decoded as =.

All ASCII characters with decimal values from 33 to 126, except =, are printable and decoded as well as encoded as themselves.

A space with ASCII value 9 and a tab with ASCII value 32, represent themselves in the decoded data.


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

import quopri


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

#Create input
input = text.encode('utf-8')

#Generate encoding
output = quopri.encodestring(input,  quotetabs = True)

#Display Encoding 


#Create input
input_decode = b'This=20text=20contains=20=C3=BCnic=C3=B6de'

#Generate Decoding
output_decode = quopri.decodestring(input_decode, header = True)

#Display Decoding

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

  • The first part of the program takes the example input and generates its quoted-printable encoding through the quopri.encodestring() function.
  • The spaces are encoded as =20 because the quotetabs parameter was True during the encoding. The program encodes Unicode characters ü and ö as =C3=BC and =C3=B6, respectively.
  • The encoded form is given as an input to the quopri.decodestring() function.
  • The displayed decoded result is identical to the example text.

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




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

View all Courses

Keep Exploring