How to find a word in a string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to find a word in a string

This is what I have tried but it's not executing as expected Name = input("Enter a string") Letter = input("Enter a letter") Repl = ("Enter a replacement") if (Name.find(Letter)==-1): print("Sorry the letter can not be found") else: print(Name.replace(Letter,Repl))

16th Jun 2020, 2:21 PM
Marble Kusanele Mpofu
Marble Kusanele Mpofu - avatar
3 Answers
+ 6
There is 1 issue in the code, see my comments: Name = input("Enter a string") Letter = input("Enter a letter") Repl = input("Enter a replacement") # input missing if Letter not in Name: # this is more convenient but no error print("Sorry the letter can not be found") else: print(Name.replace(Letter,Repl))
16th Jun 2020, 2:43 PM
Lothar
Lothar - avatar
+ 3
Correct the 3rd line. Repl=input("Enter a replacement")
16th Jun 2020, 2:36 PM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 2
Ohh eish Don't know how I missed that Thanks
16th Jun 2020, 2:49 PM
Marble Kusanele Mpofu
Marble Kusanele Mpofu - avatar