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)
