How to remove multiple character from a string in Python

3 Answers

0 votes
s = "Python is an interpreted high-level general-purpose programming language"

list_of_chars = ['i', 'o', 'n', 'e']

mapping_table = s.maketrans('', '', ''.join(list_of_chars))

s = s.translate(mapping_table)

print(s)




'''
run:

Pyth s a trprtd hgh-lvl gral-purps prgrammg laguag

'''

 



answered Nov 4, 2021 by avibootz
0 votes
import re

s = "Python is an interpreted high-level general-purpose programming language"

list_of_chars = ['i', 'o', 'n', 'e']

pattern = '[' +  ''.join(list_of_chars) +  ']'

s = re.sub(pattern, '', s)

print(s)




'''
run:

Pyth s a trprtd hgh-lvl gral-purps prgrammg laguag

'''

 



answered Nov 4, 2021 by avibootz
0 votes
s = "Python is an interpreted high-level general-purpose programming language"
 
list_of_chars = ['i', 'o', 'n', 'e']
 
for ch in list_of_chars:
    s = s.replace(ch, '')
 
print(s)
 
 
 
 
'''
run:
 
Pyth s a trprtd hgh-lvl gral-purps prgrammg laguag
 
'''

 



answered Nov 4, 2021 by avibootz
edited Jan 31, 2022 by avibootz
...