I though re.sub() is for replacemenmt. For example:
import re
print(re.sub('ab', 'ad', 'abc'))
The output is ‘adc’ of course. However, when I try this:
s = "hello world!"
print(re.sub("[^A-Za-z]", "+", s))
The output is weirdly ‘hello+world+’ instead of ‘+++++ +++++!’. It actually keeps all the letters rather than replace then with "+". Idk why…
>Solution :
remove the ^, it anchors to a single point, source:
Code:
s = "hello world! how are you?"
print(re.sub("[A-Za-z]", "+", s))
you can find it in the documentation
Output:
+++++ +++++! +++ +++ +++?