
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Morse Code Translator in Python
Morse Code Translator is used in Cryptography. It is named by Samuel F. B. Morse. By this technique we convert a message to a series of dots, commas,"-","/".
This technique is very simple. Every alphabet in English signifies a series of ".",",","/","-". We just encrypt the message from message to symbols and decrypt from symbols to English.
The dictionary is given below
'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-'}
Example
Message is PYTHON-PROGRAM Output is .--. -.-- - .... --- -. -....- .--. .-. --- --. .-. .- --
Algorithm
Encryption
Step1: Given a string, atfirst we extract each letter from the word and match with the Morse Code dictionary, then we consider the code corresponding the letter. Step2: Next step is to store the code into a variable. And we have to follow that one space should be maintained between every Morse code. Step3: Two spaces should be maintained in between every word.
Decryption
Step1: First we add a space at the end of the string. Step2: Now we traverse each letter of the message until space is not encountered. Step3: When we get space then check with Morse Code Dictionary and store in a variable. Step4: When get 2 consecutive spaces we will add another space to our variable containing the decoded string. Step5: When get last space of the message that means this is the last letter of Morse Code Generator.
Example code
# -*- coding: utf-8 -*- """ Created on Tue Oct 2 11:21:31 2018 @author: Satyajit """ # Dictionary representing the morse code chart MORSE_CODE_DICT = { 'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-' } def encryption(message): my_cipher = '' for myletter in message: if myletter != ' ': my_cipher += MORSE_CODE_DICT[myletter] + ' ' else: my_cipher += ' ' return my_cipher # This function is used to decrypt # Morse code to English def decryption(message): message += ' ' decipher = '' mycitext = '' for myletter in message: # checks for space if (myletter != ' '): i = 0 mycitext += myletter else: i += 1 if i == 2 : decipher += ' ' else: decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT .values()).index(mycitext)] mycitext = '' return decipher def main(): my_message = "PYTHON-PROGRAM" output = encryption(my_message.upper()) print (output) my_message = ".--. -.-- - .... --- -. -....- .--. .-. --- --. .-. .- -- " output = decryption(my_message) print (output) # Executes the main function if __name__ == '__main__': main()
Output
.--. -.-- - .... --- -. -....- .--. .-. --- --. .-. .- -- PYTHON-PROGRAM
- Related Articles
- Unique Morse Code Words in Python
- Converting string to MORSE code in JavaScript
- Microsoft an easy translator bookmarklet
- What are the types of the translator in compiler design?
- Python Code Objects
- Writing Efficient Python Code
- How will you Convert MATLAB Code into Python Code?
- Execute a String of Code in Python
- Packaging and Publishing Python code?
- Optimization Tips for Python Code?
- How to generate byte code file in python
- Understanding Code Reuse and Modularity in Python 3
- How do I execute a string containing Python code in Python?
- Performing Google Search using Python code?
- Best Python IDEs And Code Editors

Advertisements