Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,109 questions

40,780 answers

573 users

How to use translate() and maketrans() to replace and remove multiple characters from string in Python

Learn & Practice SQL


80 views
asked Apr 15, 2021 by avibootz
edited Apr 15, 2021 by avibootz

2 Answers

0 votes
s = "Python is an interpreted programming language"

# mapping_table()
# "aeiou" - characters to be replaced
# "AEIOU" - replacements characters
# "ag" - characters to be deleted

mapping_table = s.maketrans("aeiou", "AEIOU", "ag")

s = s.translate(mapping_table)

print(s)



'''
run:

PythOn Is n IntErprEtEd prOrmmIn lnUE

'''

 





answered Apr 15, 2021 by avibootz
0 votes
s = "Python is an interpreted programming language"
 
# mapping_table()
# "aeiou" - characters to be replaced
# "AEIOU" - replacements characters
# "ag" - characters to be deleted

mapping_dictionary = {'a': 'A', 'e':'E', 'i': 'I', 'o': 'O', 'u': 'U', 'a': None, 'g': None}
 
mapping_table = s.maketrans(mapping_dictionary)
 
s = s.translate(mapping_table)
 
print(s)
 
 
 
'''
run:
 
PythOn Is n IntErprEtEd prOrmmIn lnUE
 
'''

 





answered Apr 15, 2021 by avibootz
edited Apr 15, 2021 by avibootz
...