python challenge question (string manipulation)

Terry Reedy tjreedy at udel.edu
Wed Mar 29 17:17:12 EST 2006


"John Salerno" <johnjsal at NOSPAMgmail.com> wrote in message 
news:LiBWf.1856$No6.41969 at news.tufts.edu...
> John Salerno wrote:
>
>> It works, but is there a better way to shift the letters of the alphabet
>> for 'code'? I remember a method that did this for lists, I think, but I
>> can't remember what it was or if it worked for strings.
>
> Ah ha! This is cleaner:
>
> alphabet = string.ascii_lowercase
> code = string.ascii_lowercase[2:] + string.ascii_lowercase[:2]
>
> Yet it still seems kind of verbose. But since that's the official
> solution, I guess there's no other way to shift the characters in a 
> string?

The above would look less verbose if the second line 'paid attention' to 
the first and were written as the slightly more efficient

code = alphabet[2:] + alphabet[:2]

An alternative is shifted access.  alphabet[(i+24)%26]

Terry Jan Reedy







More information about the Python-list mailing list