Finding the shortest string in a string of words in python

后端 未结 4 907
后悔当初
后悔当初 2021-01-28 02:30

I want to write a function that would return the length of the shortest string in a string of words. Sample: \"I eat apples\" would return 1 since \"I\" is the shorted string. T

4条回答
  •  失恋的感觉
    2021-01-28 03:08

    A function to estabilish the lenght of the smallest word in a text

    def lenmin(text):
        return len(min(text.split()))
    
    lenmin("I eat apples")
    

    This will return 1

    Using lambda

    >>> lenmin = lambda t: len(min(t.split()))
    >>> lenmin("you eat apples")
    2
    

提交回复
热议问题