String with larger and smaller number of characters

Asked

Viewed 1,324 times

0

I wish to return to the greater and the lesser string.

Look at:

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 writes, I would like to take the largest and the smallest string and print them on screen.

  • But the user will enter a letter, number or text?

  • any type of string, the smallest and longest string strings would be printed. Ex: 23123 Asd da as . The output would be: minor = . major = 23123

1 answer

1


Just create a list, very similar to what you have already done:

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 function return len and not only the content of the text.

menor = min(textos, key=len)
maior = max(textos, key=len)
  • Thank you very much, it worked perfect^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.