Python-courses-python-exercises

Exercise 46

Write a Python program as a function which takes as parameter a string s and which returns the list of all the words contained in s whose length is greater than or equal to 8. Example if s = 'Python is oriented programming language. Python is the most used programming language'. The function returns the words list :

['oriented', 'programming', 'language', 'programming', 'language']

Solution

def selectWords(s):
# initialize the list we are looking for
L = []

# getting the list of word contained in s
listWords = s.split()

# select all word of length >= 8
for word in listWords:
if len(word) >=8:
L.append(word)
return L

# testing algorithm
s = 'Python is oriented programming language and the most used programming language '

print('List of all word whose lenght is >= 8 is list_word = ' , selectWords(s))
# The output is :
# List of all word whose lenght is >= 8 is list_word = ['oriented', 'programming', 'language', 'programming', 'language']

Younes Derfoufi
my-courses.net

Leave a Reply