469,600 Members | 2,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Incrementing letters

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
Jul 19 '05 #1
9 7005
Am Freitag, 27. Mai 2005 13:31 schrieb Michael:
if(C == 'z') C='a';
else C++;


if C == "z":
C = "a"
else:
C = chr(ord(C)+1)

--
--- Heiko.
see you at: http://www.stud.mh-hannover.de/~hwundram/wordpress/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBClyLcf0bpgh6uVAMRApeFAJsG0SdfKvVZzqV/HApjdYpZrs7x8gCeOccN
bZyo0OUdxVy5be+U6pHsA5M=
=RZOy
-----END PGP SIGNATURE-----

Jul 19 '05 #2
Heiko Wundram wrote:
Am Freitag, 27. Mai 2005 13:31 schrieb Michael:
if(C == 'z') C='a';
else C++;

if C == "z":
C = "a"
else:
C = chr(ord(C)+1)

According to the OP's problem (with the assumption that only characters
from a-z are given) he might even try a lil LC:
s = "shiftthis"
''.join([chr(((ord(x)-ord('a')+1)%26)+ord('a')) for x in s])

'tijguuijt'
HTH,
Wolfram
Jul 19 '05 #3
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( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ',
'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWX YZA') 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.
Jul 19 '05 #4
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(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ',
'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWX YZA')
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
Jul 19 '05 #5
On Fri, 27 May 2005 16:10:32 +0200,
Wolfram Kraus <kr***@hagen-partner.de> wrote:
Duncan Booth wrote:
> import string
> upone = string.maketrans(

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ',
'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWX YZA')
> 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


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

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jul 19 '05 #6
On 5/27/05, Michael <sl***********@hotmail.com> 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??


Here's one that works on multiple character strings, with carrying.
Rather silly, really.

<http://www.brunningonline.net/simon/blog/archives/001787.html>

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #7
Dan Sommers wrote:
Wolfram Kraus <kr***@hagen-partner.de> wrote:
Duncan Booth wrote:
>> import string
>> upone = string.maketrans(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ',
'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWX YZA')

>> 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.
Jul 19 '05 #8
Dan Sommers wrote:
On Fri, 27 May 2005 16:10:32 +0200,
Wolfram Kraus <kr***@hagen-partner.de> wrote:

Duncan Booth wrote:


>>import string
>>upone = string.maketrans(

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST UVWXYZ',
'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTU VWXYZA')
>>string.translate("I've got a string s....", upone)
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 ]

'bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX YZa'

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?
Jul 19 '05 #9
On 27 May 2005 10:52:36 -0400,
Dan Sommers <me@privacy.net> wrote:
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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Edward K. Ream | last post: by
11 posts views Thread by John Velman | last post: by
3 posts views Thread by Mothra | last post: by
8 posts views Thread by JD via AccessMonster.com | last post: by
9 posts views Thread by subramanian100in | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.