How do I check if a STRING has more than 3 characters?

Asked

Viewed 52 times

-3

I need to check if a string has more than 3 characters, how do I do this?

I’ve tried it like this:

nome = input('Digite um nome: ')
a = [i for i in enumerate(nome)]
print([i for i, y in a])
if a > 3:
    print('Válido!')
else:
    print('Inválido!')

Is making a mistake:

Traceback (most recent call last):
  File "/home/hyago/projetos/exerciciosPythonBrasil/EstruturaDeRepeticao/ex3.py", line 4, in <module>
    if a > 3:
TypeError: '>' not supported between instances of 'list' and 'int'```

Como resolvo isso?
  • 3

    a is a list of tuples. You cannot compare this to an integer. To check whether a name has more than 3 characters just do len(nome)>3

  • Thank you very much Lucas, it helped me a lot!

1 answer

4


Doing an > operation between an int and a list makes no sense.

If I remember correctly to do this in python would be:

if len(a) > 3:

Browser other questions tagged

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