>>> import re >>> s="The Strings" >>> s=re.sub("\w","#",s) >>> s '### #######' >>> s='Hello' + "her mom shirt" >>> s "Helloher mom shirt" >>> re.sub("\w","#",s) "######## ###'# #####"
---- Edit
OK, Now I understand that you want the result to be from a Python file. Try:
import fileinput import re for line in fileinput.input(): iter = re.finditer(r'(\'[^\']+\'|"[^"]+")',line) for m in iter: span = m.span() paren = m.group()[0] line = line[:span[0]]+paren+'#'*(span[1]-span[0]-2)+paren+line[span[1]:] print line.rstrip()
This does not apply to line breaks, the form "" and only 1 or two files that I have are checked ...
In general, it is better to use a parser for this kind of work.
The best
source share