0

I am trying to modify a string , by replacing each alphabet by an alphabet which is two postions ahead of it. e.g. 'a' replaced by 'c', 'b' replaced by 'd'. The only exceptions is 'y' should be replaced by 'a' and 'z' by 'b' (loops back to start).

I have written the following code for it, which seems to serve the purpose, but the output formatting kind of spoils it.

string = "g fmnc wms bgblr rpylqjyrc gr zw fylb."
print string
for i in string:
    if i.isalpha():
        if ord(i)>120:
            print chr(ord(i)-24),
        else:
            print chr(ord(i)+2),
    else: 
        print i

Output :

g fmnc wms bgblr rpylqjyrc gr zw fylb.
i   h o p e   y o u   d i d n t   t r a n s l a t e   i t   b y   h a n d .

Expected Output:

g fmnc wms bgblr rpylqjyrc gr zw fylb.
i hope you didnt translate it by hand.

Is there any alternative, quicker approach to solve this?

5
  • 6
    from string import translate and use it. Commented Jul 23, 2013 at 0:18
  • thanks did the following as an alternate approach intab = "abcdefghijklmnopqrstuvwxyz" outtab= "cdefghijklmnopqrstuvwxyzab" trantab = maketrans(intab, outtab) print string.translate(trantab) Commented Jul 23, 2013 at 0:33
  • cs50x? Really good course :) Commented Jul 23, 2013 at 0:47
  • 1
    This is called a Caesar cipher or shift cipher by the way. There are a bunch of questions on this on SO. Commented Jul 23, 2013 at 1:49
  • @korylprince thanks that was informative. Commented Jul 23, 2013 at 3:24

3 Answers 3

3

The print i, syntax adds a space at the end of the thing that's being printed. You could solve this by putting all your characters in a list and ''.join()ing them in the end:

string = "g fmnc wms bgblr rpylqjyrc gr zw fylb."
print string
answer = []
for i in string:
    if i.isalpha():
        if ord(i)>120:
            answer.append(chr(ord(i)-24))
        else:
            answer.append(chr(ord(i)+2))
    else: 
        answer.append(i)
print ''.join(answer)

Of course, as others have suggested, string.translate will likely be far more straightforward

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a solution using string.translate:

from string import translate, maketrans, ascii_lowercase

s='g fmnc wms bgblr rpylqjyrc gr zw fylb.'

rot2=maketrans(
    ascii_lowercase,
    ascii_lowercase[2:]+ascii_lowercase[:2]
    )

print s.translate(rot2)

Comments

0

You can also use this horrific bastardization of a list comprehension.

message = "g fmnc wms bgblr rpylqjyrc gr zw fylb."
print message
print ' '.join([''.join([chr(((ord(letter)+2) % 122) + int(ord(letter)/121.0)*96) if ord(letter)>96 else letter for letter in word]) for word in message.split()])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.