By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Rearranging letters

P: 2
Hey

This is my first post here and i hope you can help :)
I would like to create a very basic easy program that can rearrange letters.
It would take the inputed word process it and print the new word out

for example if you entered the word job it would move each letter 3 spaces backwards so the printed result would be "gly"

What would be the best way to approach this

Thankyou in advance
Nov 29 '07 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Hey

This is my first post here and i hope you can help :)
I would like to create a very basic easy program that can rearrange letters.
It would take the inputed word process it and print the new word out

for example if you entered the word job it would move each letter 3 spaces backwards so the printed result would be "gly"

What would be the best way to approach this

Thankyou in advance
Make a string of ascii letters:
Expand|Select|Wrap|Line Numbers
  1. >>> import string
  2. >>> string.ascii_lowercase
  3. 'abcdefghijklmnopqrstuvwxyz'
  4. >>> letters = string.ascii_lowercase
The string index of each letter can be found by using the string method index(). Subtract 3 from the index number, and get the new letter from the string using the slice operator.
Expand|Select|Wrap|Line Numbers
  1. >>> word = 'job'
  2. >>> ''.join([letters[letters.index(letter)-3] for letter in word])
  3. 'gly'
  4. >>> 
Nov 29 '07 #2

Expert 100+
P: 511
Expand|Select|Wrap|Line Numbers
  1. >>> s="def"
  2. >>> ''.join([chr(ord(ch)-3) for ch in s])
  3. 'abc'
  4.  
Nov 29 '07 #3

P: 2
That worked great thanks alot :)

I wrote it line by line into the python command line. Is there anyway to copy it out like into a text file and just change the word whenever i want menaing i don't have to repeatly write it out for just one word

thanks in advance

Edit: That was to the first reply. I will look at the second reply now

edit again: They both worked great. Now if i can only work out how to save it i will be sitting good :D
Nov 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.