Write a python 3 function named words_in_both that takes two strings as parameters and returns a set of only those words that appear in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to".
1.Use python's set intersection operator in your code.
2.Use Python's split() function, which breaks up a string into a list of strings. For example:

sentence = 'Not the comfy chair!'
print(sentence.split())
['Not', 'the', 'comfy', 'chair!']
Here's one simple example of how words_in_both() might be used:

common_words = words_in_both("She is a jack of all trades", 'Jack was tallest of all')

Answer :

tonb

Answer:

def words_in_both(a, b):

 a1 = set(a.lower().split())

 b1 = set(b.lower().split())

 return a1.intersection(b1)

common_words = words_in_both("She is a jack of all trades", 'Jack was tallest of all')

print(common_words)

Explanation:

Output:

{'all', 'of', 'jack'}

fichoh

The program returns the words which exists in both string parameters passed into the function. The program is written in python 3 thus :

def words_in_both(a, b):

#initialize a function which takes in 2 parameters

a1 = set(a.lower().split())

#split the stings based on white spaces and convert to lower case

b1 = set(b.lower().split())

return a1.intersection(b1)

#using the intersection function, take strings common to both variables

common_words = words_in_both("She is a jack of all trades", 'Jack was tallest of all')

print(common_words)

A sample run of the program is given thus :

Learn more : https://brainly.com/question/21740226

${teks-lihat-gambar} fichoh

Other Questions