[Tutor] String switch

Alan Gauld alan.gauld at btinternet.com
Mon Oct 3 11:57:40 CEST 2011

On 03/10/11 01:33, Christopher King wrote:
> Dear Tutors,
>      I was wondering how one would make it so all the cases of all the
> strings in a python file where switched. I know that
> for individual strings, you can use .swapcase(),

So just apply swapcase() to the whole file as a string:

s = open("spam.py").read().swapcase()

 > but I'm making a program to edit others,

I don't know what that means? "edit others" what?

> so it would be easier to just do something at
> the top that would switch all other strings.

Other strings as opposed to what?
Do you want to swap case of all characters in the file?
Or only some of them? And if only some how do you distinguish
which ones?


Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list