· 5 years ago · Nov 15, 2020, 01:24 AM
1URLEncoder
2
3 BlogFAQURLDecoder.io
4
5How to encode URLs in Python
6Rajeev Singh4 mins
7
8How to encode URLs in Python
9
10URL encoding is often needed when you’re calling a remote api with additional query strings or path parameters. Any query string or path parameter placed in the URL must be properly URL encoded.
11
12URL encoding is also required while preparing data for submission with application/x-www-form-urlencoded MIME type.
13
14In this article, you’ll learn how to encode URL components in Python. Let’s get started!
15URL Encoding query strings or form parameters in Python (3+)
16
17In Python 3+, You can URL encode any string using the quote() function provided by urllib.parse package. The quote() function by default uses UTF-8 encoding scheme.
18
19Let’s see an example -
20
21>>> import urllib.parse
22>>> query = 'Hellö Wörld@Python'
23>>> urllib.parse.quote(query)
24'Hell%C3%B6%20W%C3%B6rld%40Python'
25
26Note that, the quote() function considers / character safe by default. That means, It doesn’t encode / character -
27
28>> urllib.parse.quote('/')
29'/'
30
31The quote() function accepts a named parameter called safe whose default value is /. If you want to encode / character as well, then you can do so by supplying an empty string in the safe parameter like this-
32
33>>> urllib.parse.quote('/', safe='')
34'%2F'
35
36Encoding space characters to plus sign (+) using quote_plus() function
37
38The quote() function encodes space characters to %20. If you want to encode space characters to plus sign (+), then you can use another function named quote_plus provided by urllib.parse package.
39
40>>> import urllib.parse
41>>> query = 'Hellö Wörld@Python'
42>>> urllib.parse.quote_plus(query)
43'Hell%C3%B6+W%C3%B6rld%40Python'
44
45 Read: When to encode space to plus (+) or %20?
46
47Encoding multiple parameters at once
48
49You can encode multiple parameters at once using urllib.parse.urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value. The resulting string is a series of key=value pairs separated by & character.
50
51Let’s see an example -
52
53>>> import urllib.parse
54>>> params = {'q': 'Python URL encoding', 'as_sitesearch': 'www.urlencoder.io'}
55>>> urllib.parse.urlencode(params)
56'q=Python+URL+encoding&as_sitesearch=www.urlencoder.io'
57
58If you want the urlencode() function to use the quote() function for encoding parameters, then you can do so like this -
59
60urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
61
62Encoding multiple parameters at once where one parameter can have multiple values
63
64The urlencode() function takes an optional argument called doseq. If your input can have multiple values for a single key, then you should set the doseq argument to True so that all the values are encoded properly -
65
66>>> import urllib.parse
67>>> params = {'name': 'Rajeev Singh', 'phone': ['+919999999999', '+628888888888']}
68>>> urllib.parse.urlencode(params, doseq=True)
69'name=Rajeev+Singh&phone=%2B919999999999&phone=%2B628888888888'
70
71URL Encoding in Python 2.x
72
73In Python 2.x the quote(), quote_plus(), and urlencode() functions can be accessed directly from the urllib package. These functions were refactored into urllib.parse package in Python 3.
74
75The following examples demonstrate how you can perform URL encoding in Python 2.x using the above functions.
76
77 urllib.quote()
78
79 >>> import urllib
80 >>> urllib.quote('Hello World@Python2')
81 'Hello%20World%40Python2'
82
83 urllib.quote_plus(): Encode space to plus sign (’+‘)
84
85 >>> import urllib
86 >>> urllib.quote_plus('Hello World@Python2')
87 'Hello+World%40Python2'
88
89 urllib.urlencode(): Encode multiple parameters
90
91 >>> import urllib
92 >>> params = {'q': 'Python 2.x URL encoding', 'as_sitesearch': 'www.urlencoder.io'}
93 >>> urllib.urlencode(params)
94 'q=Python+2.x+URL+encoding&as_sitesearch=www.urlencoder.io'
95
96 Also Read: How to decode URL components in Python
97
98References
99
100 Python urllib.parse.quote()
101
102 Python urllib.parse.quote_plus()
103
104 When to encode space to plus (+) or %20?
105
106 Share on social media
107
108Facebook TwitterLinkedin
109
110 Reddit
111
112URLEncoder
113
114Use our free online tool to encode any string to url encoded format.
115Try it now →
116What is URL Encoding and How does it work?
117What is URL Encoding and How does it work?
118Rajeev Singh3 mins
119How to URL Encode a String in Java
120How to URL Encode a String in Java
121Rajeev Singh2 mins
122URL Encoding a String in Javascript
123URL Encoding a String in Javascript
124Rajeev Singh2 mins
125How to URL Encode a String in Golang
126How to URL Encode a String in Golang
127Rajeev Singh3 mins
128PHP URL Encode example
129PHP URL Encode example
130Rajeev Singh2 mins
131URLEncoder
132Copyright © 2018 URLEncoderPrivacy Policy
133
134 HomeBlogFAQAboutContact
135
136 Base64 EncoderBase64 DecoderJSON FormatterASCII TableQRCodeBitCalliCoder