471,350 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Rearranging letters

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
3 7898
bvdet
2,851 Expert Mod 2GB
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
ghostdog74
511 Expert 256MB
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
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.

Similar topics

2 posts views Thread by Edward K. Ream | last post: by
19 posts views Thread by Johannes Nix | last post: by
1 post views Thread by Tor Inge Rislaa | last post: by
4 posts views Thread by db2admin | last post: by

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.