Incrementing letters

Duncan Booth duncan.booth at invalid.invalid
Fri May 27 08:22:19 EDT 2005


Michael wrote:

> Hi,
> I've got a string s, and i want to shift all the letters up by one, eg
> a->b, b->c ........ z->a
> In c++ i can do this quite simply with
> 
> if(C == 'z') C='a';
> else C++;
> 
> but i can't work out how to do this this in python??

>>> import string
>>> upone = string.maketrans(
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
    'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA')
>>> string.translate("I've got a string s....", upone)
"J'wf hpu b tusjoh t...."
>>> 

Note the difference though: the Python code does what you said you wanted, 
whereas your sample code corrupts punctuation.



More information about the Python-list mailing list