Lower and upper case function in python


I am a beginner in python and maybe this question seems simple to some but well, here I want to ask how I could ask for text by keyboard and that text that is automatically saved in uppercase?

dni = int(input("Ingrese su numero de DNI: "))
nombre = str(input("Ingrese su nombre y apellido: "))
edad = int(input("Ingrese su edad:"))
especialidad = str(input("Ingrese especialidad: "))
status = "Disponible"

It's in the specialty part I do not know if that would work, and if someone also knows how to do so that he does not identify accents, great, if he emphasizes or not that it is the same that is saved without an accent. So when looking for it in the console it is easier to find it. Thanks !!!

asked by Gerónimo Membiela 29.12.2018 в 05:57

3 answers


There is a function called replace () that allows you to modify a text or a character, so you can change the "á" to "a", although it would be easier to apply what is regular expressions, I leave you an example.

import re
Patron= '[ÁÉÍÓÚ]'
cadena=str(input("Ingrese cadena: "))
p = re.compile(Patron) 
m = p.search(cadena)
if m:
    print("Tiene tilde",cadena)
    print("No tiene tilde", cadena )

You can validate, after entering the string, that if it has a tilde, to request the string from the user again and he will know that he should not enter tilde. And to make everything uppercase you must reassign the value that returns upper ()

answered by 29.12.2018 / 07:38

For the accents and weird characters, you can use unidecode , it does not come with python I think, but you can install pip install unidecode .

unidecode will try to get the character without accent more approximate or similar to the accent.

import unidecode

especialidad = str(input("Ingrese especialidad: "))
especialidad = especialidad.upper()


Although what says Anthony Andrés is also very good, it is the fastest I think since there is no import and does not have as many things as unidecode .

answered by 29.12.2018 в 08:17

The upper () function returns an uppercase copy of the original variable, but does not modify it. You only have to replace:


for this:

especialidad = especialidad.upper()

In this way, you assign the new value to your original variable.

answered by 29.12.2018 в 07:17