Last active
August 29, 2015 14:05
-
-
Save iamaziz/670a9825bf3f3f2778ec to your computer and use it in GitHub Desktop.
recursively replace each char (substring) in *args from a given text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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