473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 7203
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: trickydicky | last post by:
Does anyone know of an easy way to increment a string by one letter at a time? What I want to be able to do is set a variable with the value "A" and then be able to use a For loop to make it...
2
by: Edward K. Ream | last post by:
From the documentation for the string module at: C:\Python23\Doc\Python-Docs-2.3.1\lib\module-string.html letters: The concatenation of the strings lowercase and uppercase described below....
11
by: John Velman | last post by:
I've used perl for a lot of 'throw away' scripts; I like Python better in principle, from reading about it, but it was always easier to just use perl rather than learn python. Now I'm writing a...
3
by: Mothra | last post by:
Here's what I'm trying to do (kill off old Unix logins): --------------------- $i=0; while (<$who>) { chomp($_); my @line = split(/\s+/, $_); # Split it into an array next unless ($line...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
10
by: Antanas | last post by:
The problem is that when AddID is used multiple times in the same select statement, it returns the same value in all places. How could I force function AddID to increment OBJECTID sequence? Here...
8
by: JD via AccessMonster.com | last post by:
I am trying to create a field where the primary key field will produce JDP- 001; where the three letters come from the first name, middle intial and last name of my table. I want to auto increment...
9
by: Lilbrat | last post by:
Im not finding anything similar in the Posts so correct me if Im wrong... Im real new to programing in Access (Been a while since I programed in anything). The question I have is.. How do you...
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.