Incrementing letters

M

Michael

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??

Regards

Michael
 
D

Duncan Booth

Michael said:
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??
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA')
Note the difference though: the Python code does what you said you wanted,
whereas your sample code corrupts punctuation.
 
W

Wolfram Kraus

Duncan said:
Michael wrote:



"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
 
D

Dan Sommers

Duncan Booth wrote:
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

And use string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ] for the
second parameter to string.maketrans.

Regards,
Dan
 
D

Duncan Booth

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:
'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.
 
R

Rocco Moretti

Dan said:
Duncan Booth wrote:


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


And use string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ] for the
second parameter to string.maketrans.

Not quite:
>>> string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ]
'bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZa'

i.e. you get 'z' -> 'A' and 'Z' -> 'a'

Another issue is locale settings and special characters - what should be
done with accented letters? Preserve letter, or shift and loose accent?
 
D

Dan Sommers

And use string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ] for the
second parameter to string.maketrans.

Oops. Thank you Duncan and Rocco for correcting my mistake.

Regards,
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top