Incrementing letters
Duncan Booth
duncan.booth at invalid.invalid
Fri May 27 11:21:40 EDT 2005
Dan Sommers wrote:
> Wolfram Kraus <kraus at hagen-partner.de> wrote:
>
>> Duncan Booth wrote:
>
>>>>>> 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
Yes, my first attempt at responding did that, but I changed it because that
makes the assumption that string.ascii_letters is in a specific order (did
you know that lowercase came first and uppercase second without checking?)
>
> And use string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ] for the
> second parameter to string.maketrans.
>
Bzzt. Wrong answer. Look closely at the middle of the string.
I did have:
>>> upone = string.maketrans(string.ascii_letters,
'z'+string.ascii_lowercase[:-1] +
'Z' + string.ascii_uppercase[:-1])
but as I said, that makes too many assumptions for my liking about the
contents of those variables.
More information about the Python-list
mailing list