String with the largest and smallest number of characters

0

I want to return the largest and smallest string .

See:

nl=str(input('Digite uma string'))

String=[]
string.append(nl)

while caractere != 'pare':
    caractere = str(input("Digite uma letra ou número "))
    string.append(caractere)

Among the entries the user types, I would like to get the largest and smallest string and print them on the screen.

    
asked by anonymous 06.03.2018 / 00:59

1 answer

2

Just create a list, much like you already did:

textos = []
while True:
    texto = input("Entre com um texto:")
    if texto == 'pare':
        break
    textos.append(texto)

Then, to find the smallest and largest text, just use the native functions min and max , making use of the parameter key passing the function len . This way, you will get the smallest and largest text in relation to the return of the len function and not only the content of the text.

menor = min(textos, key=len)
maior = max(textos, key=len)
    
06.03.2018 / 01:18