Skip to content

Instantly share code, notes, and snippets.

@iamaziz
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamaziz/670a9825bf3f3f2778ec to your computer and use it in GitHub Desktop.
Save iamaziz/670a9825bf3f3f2778ec to your computer and use it in GitHub Desktop.
recursively replace each char (substring) in *args from a given text
def rep_chars(txt, *args):
"""recursively replace each char (substring) in *args from a given text"""
chars = [str(c) for c in args]
if len(chars) < 1:
return txt
else:
txt = txt.replace(chars.pop(), ' ' )
return rep_chars( txt, *chars ) # repeat rep_chars() until *args is None
a = 'abcdefgh'
c1 = 'b'
c2 = 'fg'
rep_chars(a, c1, c2) # out: 'a cde h'
# see it in action: http://goo.gl/Fd0ZHW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment