Python-courses-download-Python-courses-s-Python-courses

Exercise 70

Write a python program which extract from a given string s , the list of all words whose len is less than or equal to 4. Example if s = "The Python programming language is open source and very easy to learn" , the program must returns the list

L = ['The', 'is', 'open', 'and', 'very', 'easy', 'to']

Solution

def listWords(s):

# initializing the list we want to get
list_word = []
# converting s into a list
L = s.split()
for word in L:
if (len(word) <= 4):
list_word.append(word)
return list_word

# Example
s = "The Python programming language is open source and very easy to learn"
print(listWords(s)) # display: ['The', 'is', 'open', 'and', 'very', 'easy', 'to']

Younes Derfoufi
my-courses.net

Leave a Reply