Incrementing letters
Wolfram Kraus
kraus at hagen-partner.de
Fri May 27 10:10:32 EDT 2005
Duncan Booth wrote:
> 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.
Wow, that's quite nice. You really learn something new every day :-)
A minor improvement: Use string.ascii_letters as the first parameter for
string.maketrans
Wolfram
More information about the Python-list
mailing list