Home > Back-end >  Python input a fruit in the list and count how many words
Python input a fruit in the list and count how many words

Time:11-14

So lets say I have a list like this

fruit = ['strawberry', 'orange', 'banana', 'mango', 'cherry']

I want the user to input a name of the fruit in the list and count how many words the inputed fruit has. something like this below

input the fruit name: orange
orange has 6 words

I would really appreciate the help!

CodePudding user response:

my_list = ['strawberry', 'orange', 'banana', 'mango', 'cherry']
your_fruit = input("input the fruit name:")
if your_fruit in my_list:
    count = len(your_fruit)
    print(your_fruit, " has ", count, "letters")
else:
    print(your_fruit, " is not in ", my_list)

CodePudding user response:

You can use len(string) Function.

orange_name = 'orange'
print(len(orange_name))
  • Related