Coders Packet

Caesar Cipher Conversion by using Python

By Harshith Appari

How to convert a given text into the " Caesar Cipher " text by using Python and learn about what is meant by Caesar Cipher substitution technique

Hello everyone, In this tutorial, we will learn what caesar cipher means and how to convert the given text into caesar cipher text by simply using Python.

Caesar Cipher :

Sir "Julius Ceasar invents it" is primarily used in the army and other professions where the 3rd person should not know the information when two persons are speaking.

It is a substitution technique.

The method behind this is whenever the string or word is given, first find the Number of the alphabet around all the 26 alphabets and add 3 to the

Number, and cross-check where the new value ( added 3 to the original value ) is matched with the alphabet and replace the old alphabet with the new alphabet.

So now we are doing this in Python:

import string
alpha = list(string.ascii_uppercase)
d={}
for i in range(0,len(alpha)):
    d[alpha[i]]=i+1
keys=list(d.keys())
values=list(d.values())
a=input("Enter the string : ")
s=a.split()
k=[]
for i in s:
   k.append(i.upper()) 
cc=[]
for i in k:
    s=""
    for j in i:
        d=keys.index(j)
        s1=(d%26)+3
        if s1>26:
            s1=(d-26)+3
            s=s+keys[s1]
        else:
            s=s+keys[s1]
    cc.append(s)
print("The Caesar Cipher text is ")
for i in cc:
    print(i,end=" ")

 

First, I am importing the module called String to change the lowercase letters into uppercase, which can be done using the function called" String.ascii_uppercase ".

And then create the empty dictionary to get the alphabets and their respective numbers from 1 to 26.

And by using for loop, I am assigning the key-value pair to the dictionary like keys are alphabets and values are numbers so that I increment the value of i.

And make all the keys into one list using "d.keys()."

And similarly making all the values into one list by using the "d.values()".

And now I am asking the input from the user by using the input function.

And then splitting the strings into words by using the split() function.

And now creating the empty list and assigning the variable as k.

Now by using the for loop, we are changing the word into uppercase and then appending it to the empty list k and making the empty list as cc.

And now, for each word in list k.

By using for loop, first, create an empty string as s.

And to retrieve each letter in the word, we can use again the for loop.

And now getting the index of the alphabet from the keys list because the alphabets are in the keys list and assign it to the variable d.

And to call the +3 of the alphabet, we are using the modulus symbol to get the remainder and add the +3 and assign the answer to the s1 variable.

if the number s1 is more than 26, we are subtracting the from the total alphabets and adding +3, and appending it to the empty String s
or else, if the s1 is less than 26, we are directly adding to the empty String s.

And append to the cc.

And by using the for loop to print the words of the caesar cipher text and to retrieve the answer as sentences, we are using the " end " function.

 

Enter the string : meet me after the toga party
The Caesar Cipher text is 
PHHW PH DIWHU WKH WRJD SDUWB 

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Harshith Appari (Harshith)

Download packets of source code on Coders Packet