Shifted Alphabet Code – Translator – Python

In this post we will make a program that takes a text file as input and will print a code where the characters are shifted.


Plan

  • Open text file
  • List of characters
  • Translate text

Open text file

#Save text
f=open('text.txt','r')
Text=f.read()
f.close()

This opens a text file called ‘Text.txt’ and saves the content


List of characters

#List of characters
chars=[]
string=str('abcdefghijklmnopqrstuvwxyz .,?!:;""\nABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#@'+"'")
for x in string:
    chars.append(x)

This makes an array with every character you want in your code.


Translate text

NewText=''
for y in Text:
    if y in chars:
        index=(chars.index(y))+2
        if index > (len(chars)-1):
            index=index-len(chars)
        NewText=NewText+str(chars[index])
print(NewText)

This makes a new string where the characters are shifted along the array. If the index is to high, it takes away the length of the array so the index won’t be out of range.


Full code

#Save text
f=open('text.txt','r')
Text=f.read()
f.close()

#List of characters
chars=[]
string=str('abcdefghijklmnopqrstuvwxyz .,?!:;""\nABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#@'+"'")
for x in string:
    chars.append(x)

#Translate text
NewText=''
for y in Text:
    if y in chars:
        index=(chars.index(y))+2
        if index > (len(chars)-1):
            index=index-len(chars)
        NewText=NewText+str(chars[index])
print(NewText)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s