I'm working on The Python Challenge. The current code iterates over a 98,766-character string before printing an 8-character string. Current execution time is between 7 and 10 seconds.
How can I optimize my current code and what rookie mistakes am I making that may be adding to the execution time?
import urllib.request
import re
url = 'http://www.pythonchallenge.com/pc/def/ocr.html'
x = urllib.request.urlopen(url).read().decode('utf-8')
text = (re.findall(r'<!--(.*?)-->', x, re.DOTALL))[1]
# source contains two adjacent html comments
# returns second comment
s = ''
for i in text:
if text.count(i) < 10:
s += i
print(s)