How do I check if it’s uppercase, lowercase, or mixed with lowercase?

Asked

Viewed 831 times

2

    print('-----')
    print('TESTE')
    print('-----')

    print()

    lista = ['Joao', 'Joaozinho', 'Rafael']
    print(lista)
    var = str(input('Digite algum dos nomes da lista:'))

    if var == 'Joao' or var == 'João' or var == 'joao' or var == 'JOao' or var == 'JOAO' or var == 'JOão':
    print('Qualquer coisa')

    elif var == 'Joaozinho' or var == 'JOAOZINHO' or var == 'joaozinho' or var == 'joaoZINHO':
    print('Qualquer coisa')

    elif var == 'RAFAEL' or var == 'Rafael' or var == 'RAfael':
    print('Qualquer coisa') 

Basically, what I want to do is the following, I want to check these exemplary names without having to make these various conditions, for example the "RAFAEL", "Rafael", and "Rafael", even with some capital letters mixed with lowercase, as for example this last "Rafael"How do I do this?

2 answers

4


You don’t have to! Simply convert the variable to lowercase (all tiny) using .lower(), and then compare with the lowercase version of the name.

Thus:

if var.lower() == "joao":
    print("Qualquer coisa")
elif var.lower() == "joaozinho":
    print("Qualquer coisa")
elif var.lower() == "rafael":
    print("Qualquer coisa")
  • We had the same reasoning hahah

  • Thanks Matheus, solved my problem!

2

I don’t understand exactly what you want, but I had the same reasoning as @Matheus Avellar

print('-----')
print('TESTE')
print('-----')
print()
lista = ['Joao', 'Joaozinho', 'Rafael']
print(lista)
var = raw_input('Digite algum dos nomes da lista:\n')
print('Voce digitou         =>'+'"'+var+'"'+' tudo misturado minusculo com maisculo'+'\n'+'Mas eu exibo correto =>'+'"'+var.lower().title())+'"'

See working on Ideone

  • Thank you also Magic!!

Browser other questions tagged

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