How to replace multiple characters in a string with other characters using Python

2 Answers

0 votes
s = "Python- prog$$$$ra-mming! la!ng-uage!"

s = s.replace('!', ' ').replace('$', '').replace('a', 'A').replace('-', '+')

print(s)

 
 
'''
run:
 
Python+ progrA+mming  lA ng+uAge 
 
'''
  

 



answered Oct 21, 2024 by avibootz
0 votes
def ReplaceMultipleCharacters(s, chars_to_replace, replacement_chars):
    i = 0
    
    for ch in chars_to_replace:
        s = s.replace(ch, replacement_chars[i])
        i += 1
    
    return s


s = "Python- prog$$$$ra-mming! la!ng-uage!"

chars_to_replace = "-$!"
replacement_chars = "+* "

s = ReplaceMultipleCharacters(s, chars_to_replace, replacement_chars)


print(s)

 
 
'''
run:
 
Python+ prog****ra+mming  la ng+uage 
 
'''
  

 



answered Oct 21, 2024 by avibootz
...