Skip to content Skip to sidebar Skip to footer

Python Code To Find A Total Number Of Shortest Words In A String

I am searching for a python code that finds the total number of shortest word in a string. For example if the string is 'The play 's the thing wherein I'll catch the conscience of

Solution 1:

input_string = "The play 's the thing wherein I'll catch the conscience of the king."

To count number of words:
print(len(input_string.split()))

Output:
13

To count number of words of just three letter or less:
print(len([x for x in input_string.split() if len(x) <= 3]))

Output:
6

If you want the list of words of just three letter or less, exclude the len() function.
print([x for x in input_string.split() if len(x) <= 3])

Output:
['The', "'s", 'the', 'the', 'of', 'the']


Post a Comment for "Python Code To Find A Total Number Of Shortest Words In A String"