Chris Clifton via Tutor wrote:

> I have been practicing with strings.  Splitting them, joining them,
> changing case.  All has been going well but came across a exercise in one
> of the code practice sites that has you changing the case of different
> characters in a string.  Anything in upper case is converted to lower case
> and anything in lower case is converted to upper.  The solution I have
> created seems to work but I have not been able to figure out how to join
> the string back together.   String I'm using is "This Is A Test!" to be
> changed to "tHIS iS a tEST!".

You'll learn more if you try to understand the approaches shown in Danny's 
and Alan's posts, but there is also a ready-to-use string method:

>>> "This Is A Test!".swapcase()
'tHIS iS a tEST!'

I have no idea where you'd need such a method other than to "cheat" in 
exercises like the one you saw...

There is also a method to make arbitrary replacements that is much more 
useful for solving real-world problems:

>>> replacements = str.maketrans({"Ü": "Ue", "ü": "ue"})
>>> "Übel wütet der Gürtelwürger!".translate(replacements)
'Uebel wuetet der Guertelwuerger!'

>>> import string
>>> swap = str.maketrans(string.ascii_uppercase + string.ascii_lowercase, 
string.ascii_lowercase + string.ascii_uppercase)
>>> "This Is A Test!".translate(swap)
'tHIS iS a tEST!'


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to