[Tutor] need to use # sign

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 14 Aug 2002 14:09:33 -0700 (PDT)


On Wed, 14 Aug 2002, Watt III, Glenn wrote:

> ok im writing a cgi and i need to use the # sign in a url. i tried
> putting one of these guys down \# but that didnt seem to help like it
> does with quotes. is there any way to keep it from reading as comment?

There's a different form of escape character to protect weird characters
in URLs.  Try the 'urllib.quote()' function:

###
>>> urllib.quote("#some_text")
'%23some_text'
###


If the url contains spaces, we can use the quoteplus() function instead:

###
>>> urllib.quote("glenn watt")
'glenn%20watt'
>>> urllib.quote_plus("glenn watt")
'glenn+watt'
###


Hope this helps!