The problem is that upper
returns another string. It does not change the original (by the way, this applies to all methods that "modify" the string - in Python, strings are immutable, and these methods actually always return another modified string).
So, you have to take the return and accumulate in another string:
def consoantes_maiusculas(frase):
s = ''
for caractere in frase:
if caractere in 'bcdfghjklmnpqrstvxwyz':
s += caractere.upper() # transforma em maiúscula
else: # não é consoante minúscula, mantém como no original
s += caractere
return s
print(consoantes_maiusculas('abcdef')) # aBCDeF
Another alternative is to use join
along with a Generator Expression:
def consoantes_maiusculas(frase):
return ''.join(caractere.upper() if caractere in 'bcdfghjklmnpqrstvxwyz' else caractere for caractere in frase)
Inclusive, use join
is the most recommended in this case, for being much more efficient than the first alternative.
Thanks!!!!!!!!!!
– João
@If the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem
– hkotsubo
@hkotsubo, very calm at this time, I am also in the race :D
– Augusto Vasques