You should take a look at using urlencode.

from urllib import urlencode

base_url = "http://en.wikipedia.org/w/api.php?"

arguments = dict(action="query",
                 meta="globaluserinfo",
                 guiuser="$cammer",
                 guiprop="groups|merged|unattached",
                 format="json")
url = base_url + urlencode(arguments)

If you don't need to build a complete url you can just use the quote function for a single string:

>>> import urllib
>>> urllib.quote("$cammer")
'%24cammer'

So you end up with:

r['guiprop'] = urllib.quote(u'groups|merged|unattached')
r['guiuser'] = urllib.quote(u'$cammer')
Answer from Nathan Villaescusa on Stack Overflow
🌐
W3Schools
w3schools.com › python › gloss_python_escape_characters.asp
Python Escape Characters
An escape character is a backslash \ followed by the character you want to insert. An example of an illegal character is a double quote inside a string that is surrounded by double quotes: You will get an error if you use double quotes inside ...
🌐
W3Schools
w3schoolsua.github.io › python › python_strings_escape_en.html
Python - Escape Characters. Lessons for beginners. W3Schools in English
Python Examples Python Compiler Python Exercises Python Quiz Python Bootcamp Python Certificate ... To insert characters that are illegal in a string, use an escape character.
🌐
W3Schools
w3schools.com › tags › › ref_urlencode.asp
HTML URL Encoding Reference
URL encoding converts characters into a format that can be transmitted over the Internet.
🌐
SSOJet
ssojet.com › escaping › url-escaping-in-python
URL Escaping in Python | Escaping Techniques in Programming
Always escape characters that aren't alphanumeric or part of the unreserved set (-, ., _, ~) when constructing URLs dynamically. Python's urllib.parse module is indispensable for handling Uniform Resource Locators (URLs).
🌐
W3schools
w3schools.tech › tutorial › python › python_escape_characters
Python - Escape Characters - Python Strings - W3schools
An escape character is a backslash (\) followed by the character you want to insert. It tells Python, "Hey, the next character is special.
🌐
MojoAuth
mojoauth.com › escaping › url-escaping-in-python
URL Escaping in Python | Escaping Methods in Programming Languages
In the context of URL escaping, specific characters such as spaces, punctuation marks, and reserved characters require encoding. The most commonly used special characters include: ... Exclamation mark: Encoded as ! ... In Python, the urllib.parse module provides functions to perform URL encoding and decoding.
🌐
W3Schools
w3schools.com › tools › tool_url_encoder.php
URL Encode/Decode - W3Schools
Line Sort/Dedupe Escape/Unescape URL Parser Word Counter Emoji Picker Encoding Detector Find and Replace Whitespace Cleaner Character Counter Text to Binary Morse Code Translator NATO Phonetic Alphabet Online Notepad Text to Speech
Find elsewhere
🌐
Readthedocs
w3lib.readthedocs.io › en › latest › _modules › w3lib › url.html
w3lib.url — w3lib 2.4.1 documentation
_token = r"[{}]+".format( re.escape( "".join( _char - # Control characters. set(map(chr, range(32))) - # tspecials and space. set('()<>@,;:\\"/[]?= ') ) ) ) # RFC 822 quoted-string, without surrounding quotation marks. _quoted_string = r"(?:[{}]|(?:\\[{}]))*".format( re.escape("".join(_char - {'"', "\\", "\r"})), re.escape("".join(_char)) ) # Encode the regular expression strings to make them into bytes, as Python 3 # bytes have no format() method, but bytes must be passed to re.compile() in # order to make a pattern object that can be used to match on bytes.
🌐
Quora
quora.com › How-do-I-escape-a-unicode-URL-in-Python
How to escape a unicode URL in Python - Quora
Answer: If you have a unicode string you can do this: [code python] from urllib2 import quote escaped_string = quote(unicode_string.encode('utf-8')) [/code] All those � are a telltale of UTF-8. Unicode strings in python are "raw" unicode, so make sure to .encode() and .decode() them as approp...
🌐
Medium
medium.com › @ericvanrees › python-workout-url-encode-specicharacters-bba47e03540d
Python Workout — URL-encode special characters | by Eric van Rees | Medium
September 28, 2022 - This is explained in this overview of Python’s built-in functions. So, the URL-encoding operation is done using the ord() and hex() functions, but the main challenge for this exercise was to find a way to filter a string for letters and numbers. Turns out there are two functions that do exactly this: .isalpha() and .isdigit(). Using these in an if/else statement, you can easily filter a string with multiple special characters, such as “ABC999zz#~€&/()=?¿”.
🌐
W3Schools
w3schools.com › HTML › html_urlencode.asp
HTML URL Encoding
The default character-set in HTML5 is UTF-8. For a complete reference of all URL encodings, visit our URL Encoding Reference. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
URLDecoder
urldecoder.io › python
URL Decoding query strings or form parameters in Python | URLDecoder
>>> import urllib.parse >>> encodedStr = 'Hellö Wörld@Python' >>> urllib.parse.unquote(encodedStr) 'Hellö Wörld@Python'
🌐
Jehiah
jehiah.cz › a › guide-to-escape-sequences
The Definitive Guide to HTML, URL and Javascript Escaping | jehiah.cz
August 27, 2010 - url = "http://test.example/blog/" + post_name url = "http://test.example/page?msg=" + message · Always think of your values as having #, ?, ;, &, ../, % or any whitespace character. Any of these need to be % escaped. In javascript use encodeURIComponent() (note: do not use escape() for this) In python use urllib.quote() or urllib.urlencode()
🌐
Python
docs.python.org › 3 › library › urllib.parse.html
urllib.parse — Parse URLs into components — Python 3.14.6 ...
urllib.parse.unquote(string, encoding='utf-8', errors='replace')¶ · Replace %xx escapes with their single-character equivalent.