Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: how to remove certain characters

how do i write a function removeThese(stringToModify,charsToRemove) that will return a string which is the original stringToModify string with the characters in charsToRemove removed from it.

like image 987
Eric B Avatar asked Dec 12 '22 19:12

Eric B


2 Answers

>>> s = 'stringToModify'
>>> rem = 'oi'
>>> s.translate(str.maketrans(dict.fromkeys(rem)))
'strngTMdfy'
like image 198
SilentGhost Avatar answered Jan 04 '23 10:01

SilentGhost


>>> string_to_modify = 'this is a string'
>>> remove_these = 'aeiou'
>>> ''.join(x for x in string_to_modify if x not in remove_these)
'ths s  strng'
like image 25
DisplacedAussie Avatar answered Jan 04 '23 08:01

DisplacedAussie