>>> str = "this is an example string. ok ????" >>> import re >>> re.sub("(.{6})", r"\1#", str) 'this i#s an e#xample# strin#g. ok #????'
Update:
Typically, a period matches all characters except newlines. Use re.S to match dots with all characters, including newlines.
>>> pattern = re.compile("(.{6})", re.S) >>> str = "this is an example string with\nmore than one line\nin it. It has three lines" >>> print pattern.sub(r"\1#", str)
this i#s an e#xample# strin#g with#
more #than o#ne lin#e
in i#t. It #has th#ree li#nes
source share