Removing reference numbers from a string

Viewed 19

How would you remove a reference number from a string, e.g.

text = "It is known that bananas are yellow [1] and tomatoes are red [2]."

output:

"It is known that bananas are yellow and tomatoes are red."

Would it be regular expressions or anything else?

EDIT: I don't see I could be more precise about the question. Thanks for the answer.

1 Answers

You can do like this,

In [8]: import re

In [9]: re.sub("(\[\d\])", "", text)
Out[9]: 'It is known that bananas are yellow  and tomatoes are red .'
Related