Prefix

Report a typo

Find out for each word in a sentence whether it has a given prefix.

Nothing fancy: a case-sensitive solution on a space-split text will do (there is no need to switch the case or deal with punctuation marks).

Print all the matched words preserving the order.

Input: a prefix and a sentence on separate lines.

Output: words with the given prefix on separate lines.

Checking whether a string starts with a specific substring is one of the most common tasks, which is why Python has a string method called str.startswith(). It is equally important to know about it and to be able to implement this algorithm yourself. You are supposed to write your own function here. Seize a chance to test your strength!

Sample Input 1:

proto
We know so many words: proton and prototype and protoplasm and Protozoa.

Sample Output 1:

proton
prototype
protoplasm
Write a program in Python 3
def has_prefix(word, prefix):
pass


prefix = input()
words = input().split()

for word in words:
if has_prefix(word, prefix):
print(word)
___

Create a free account to access the full topic