In Python, how do I .find the '#'

I am trying to write code that can handle html anchor tags.  I need to test a string to see if it contains the '#'.  The code is returning URLs without '#' in them.

Thanks in advance
currTestIndex = currURL.find("#")
    if(currTestIndex == -1 or currTestIndex >= len(currURL)):
       print currURL
       return currURL

Open in new window

Who is Participating?
jonbergAuthor Commented:
Sorry.  I switched to using the "re" library.  Maybe it wasn't working for me because I am using version 2.4.3 of Python.

Thanks though.
import re

rePound = re.compile("#")

if is not None:
   print currURL
   return currURL

Open in new window

Your code works for me.  Can you post a larger section of your program, or show an example that doesn't work?
You may be interested in the standard module urlparse (; it was renamed/moved to urllib.parse in Python 3). Try the snippet below. It writes:

ParseResult(scheme='http', netloc='', path='/release/2.4.3/lib/module-urlparse.html', params='', query='', fragment='xyz')
('http', '', '/release/2.4.3/lib/module-urlparse.html', '', '', 'xyz')

import urlparse

url = ''

x = urlparse.urlparse(url)

print x.scheme
print x.netloc
print x.path
print x.fragment
print x

# The ParseResult class is actually derived from the tuple class.
print tuple(x)

# Construct the new URL from the parts.
nu = urlparse.urlunparse((x.scheme, x.netloc, x.path, '', '', ''))
print nu

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.