[Tutor] Fwd: converting upper case to lowercase and vice-versa

Peter Otten __peter__ at web.de
Fri Mar 15 17:22:34 CET 2013


Bod Soutar wrote:

> mystring = "THIS is A string"
> newstring = ""
> for item in mystring:
>     if item.isupper():
>         newstring += item.upper()
>     else:
>         newstring += item.lower()
> 
> print newstring

This does nothing the hard way as newstring and mystring are equal ;)

If you accidentally swapped the if-suite and the else-suite -- there's the 
swapcase() method:

>>> mystring = "THIS is A string"
>>> print mystring.swapcase()
this IS a STRING




More information about the Tutor mailing list