cadenadeprueba="Ejemplo de frase con muchos caracteres"
for i in cadenadeprueba:
seleccionPalabra=cadenadeprueba.find(i)
if seleccionPalabra//2==0:
i.upper()
print(cadenadeprueba)
I want to make a string that has the uppercase and lowercase letters altered, but I don't know how to do it, I have tried this and many other ways, but usually it does not change any letter or it gives an error as in this case, can someone help me?
I think you have the idea: change the even characters to uppercase, but you have some problems in your code:
find()
, at least as you are calling it, is always returning the first occurrence of the character you are iterating through.//
it's basically "floored quotient of x and y", you actually need the modulus%
i.upper()
, it only capitalizes the characteri
and does not make any changes to the original string.The simplest way, and similar to what you've tried, is to do something like this:
Basically:
cadenadeprueba
into a list of charactersif i % 2 == 1
, we pass the character to uppercase and replace the same one from the list.nueva_cadenadeprueba = "".join(lista_chars)
we join all the characters in a new string.Technically speaking we should do something like this better, to guarantee case sensitivity:
Well apparently they already showed you a way to do it, anyway I will give an example of another way:
I hope it helps you ;) Greetings.
I leave other methods somewhat different from those already provided:
method 1
method 2
method 3
They all return: