How to erase letters at any position in a string in python? -
for example, string = 'xx22xx_1x_xxxx-xxxx'
, x
can letters. want delete first 2 positions letters xx
, seventh position 1
, in order newstring = '22xx_x_xxxx-xxxx'
. function erase letters @ specific positions?
this it:
def erase(string, positions): return "".join([y x,y in enumerate(string) if x not in positions])
demo:
>>> s='xx22xx_1x_xxxx-xxxx' >>> erase(s, (0,1,7)) '22xx_x_xxxx-xxxx' >>>
Comments
Post a Comment