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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,919 questions

51,852 answers

573 users

How to replace multiple words in string with one word in Python

2 Answers

0 votes
s = 'Python is an interpreted high-level general-purpose programming language'
   
words_to_replace = ["programming", 'is', 'interpreted'] 
   
replace = 'WORD'
   
s = ' '.join([replace if i in words_to_replace else i for i in s.split()]) 
 
print(s)
 
        
'''
run:
 
Python WORD an WORD high-level general-purpose WORD language
        
'''

 



answered Apr 30, 2020 by avibootz
edited Apr 30, 2020 by avibootz
0 votes
import re

s = 'Python is an interpreted high-level general-purpose programming language'
   
replace = 'WORD'

s = re.sub('(programming|is|interpreted)', replace, s)
 
print(s)
 
        
'''
run:
 
Python WORD an WORD high-level general-purpose WORD language
        
'''

 



answered Apr 30, 2020 by avibootz
...