THIS MUST BE WRITTING IN PYTHON WITH SIMPLE THINGS LIKE IF, DEFINE FUNCTIONS, LI
ID: 3595064 • Letter: T
Question
THIS MUST BE WRITTING IN PYTHON WITH SIMPLE THINGS LIKE IF, DEFINE FUNCTIONS, LISTS ETC.
given a string such as ("HELLODOGMEMEPAPA")
I will need to return a list that gives me three things
Everything before the word dog which i will denote as before_dog
the word dog and three letters after it denoted as dog_3letters
and everything after the dog_3letters which i will denote as everything_after
the list will be in the form [before_dog,dog_3letters,everything_after]
so given a string such as ("HELLODOGMEMEPAPA") I will need to return the list ["HELLO", "DOGMEM", "EPAPA"]
or a string like ("HEYWHATDOGDODOD") I will need to return ["HEYWHAT","DOGDOD","OD"]
Explanation / Answer
Implemented the code as per your requirement and added comments for readability. Thank you..
code:
---------
x = raw_input("Enter input: ") #reading input from user
dog_index = x.lower().index("dog") #taking index of the word dog
before_dog = x[0:dog_index] #before dog letters
dog_3letters = x[dog_index:dog_index+6] #dog+next three letters
everything_after = x[dog_index+6:] #remaining last letters
l=[]
l.append(before_dog ) #adding to list
l.append(dog_3letters)
l.append(everything_after )
print l #result
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.