How do I strip a string given a list of unwanted characters? Python -
is there way pass in list instead of char str.strip()
in python? have been doing way:
unwanted = [c c in '!@#$%^&*(fghjkmn'] s = 'ffffofob*&%ar**^' u in unwanted: s = s.strip(u) print s
desired output, output correct there should sort of more elegant way how i'm coding above:
ofob*&%ar
strip , friends take string representing set of characters, can skip loop:
>>> s = 'ffffofob*&%ar**^' >>> s.strip('!@#$%^&*(fghjkmn') 'ofob*&%ar'
(the downside of things fn.rstrip(".png")
seems work many filenames, doesn't work)
Comments
Post a Comment