How to replace two words in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to replace two words in Python?

name =("hello world") print(name.replace("h""w" , "c""t"))

30th Jun 2020, 4:16 PM
🥀Priya Singh🥀
🥀Priya Singh🥀 - avatar
5 Answers
+ 2
I think you are trying to replace "h" with "c" and "w" with "t", in which case you should do this: name = "hello world" name = name.replace("h", "c").replace("w", "t") print(name) # "cello torld" I agree with everything else the others have already said 👍
30th Jun 2020, 4:34 PM
Russ
Russ - avatar
+ 1
name = "Hello World" def replaceWord(name, wtr): for w, ww in wtr.items(): name = name.replace(w, ww) return name name = replaceWord(name, {"H":'C', "W":'T'}) print(name)
30th Jun 2020, 5:01 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
name =("hello world") print(name.replace("h","w",1).replace("u","h",2)) not work
1st Jul 2020, 8:48 AM
🥀Priya Singh🥀
🥀Priya Singh🥀 - avatar
+ 1
Hi
1st Jul 2020, 4:43 PM
INVISIBLE
INVISIBLE - avatar
0
🥀Priya Singh🥀 What is the output you want?
1st Jul 2020, 12:57 PM
Russ
Russ - avatar