Need a Python program Singular to Plural

2.2k Views Asked by At

Hi Need a simple Python program to accept a list of 3 items. word_list = ['apple', 'berry', 'melon'] Using a function to convert singular to plural. If item ends with 'y', should replace that with 'ies'. Thanks so much

2

There are 2 best solutions below

0
Miles Kent On BEST ANSWER

Just make it so that it appends "s" unless the word ends with "y" or some other exception:

def plural(word):
    wordlist = []
    for char in word:
        wordlist.append(char)
    if word[len(word)-1] == "y":
        wordlist[len(word)-1] = "ies"
    else:
        wordlist.append("s")
    word = ""
    for i in wordlist:
        word+=i
    return word
print(plural("STRING"))


1
Rajat Mishra On

You can use inflect package to produce the plural form.

In [109]: import inflect

In [110]:  p = inflect.engine()

In [111]: print([p.plural(word) for word in word_list])
['apples', 'berries', 'melons']