As others said, you should simply use a regular expression that matches any number, such as r"\d"
or r"\d+"
. However, for learning purposes, here is the answer to what you did ask.
The closest useful equivalent of "variable expansion" is the string formatting operator:
cleanText = re.sub('%d.' % number, line).strip()
You could also use str(number) + '.'
to achieve the same effect. There are several more problems with your code:
your loop is wrong; if you're iterating over
range(1, 10)
, then you don't need to incrementnumber
manually.you probably meant
range(1, 11)
..
in regular expression syntax matches any characters; you want\.
.
A cleaned-up version might look like this:
cleanText = line.strip()
for i in xrange(1, 11):
cleanText = re.sub(r'%d\.', '' , cleanText)