You might have seen morse code usage in navy scenes of many movies to communicate messages. We are talking about the same morse code here, but the only difference is that we are going to write a Python program to translate from English to Morse code and vice versa.

About Morse code

Morse code has different patterns for each English alphabet, number, punctuation, and non-Latin characters. Once you know the morse code patterns for different characters, it will be easy to encode and decode them. You can refer Wikipedia page of morse code for more details and patterns. In this tutorial, we will learn how to encode plain English text into morse code and vice versa. We will be using English alphabets, numbers, and punctuation while encoding the decoding. If you want to add more types of characters, you can easily do it once you learn the base of encoding and decoding. One thing to remember is that both uppercase and lowercase alphabets have the same morse code pattern. This is because the morse code is mainly used for communication which doesn’t care about alphabet cases like everyday conversations. Let’s get into the coding part for encoding and decoding.

English to Morse code

The algorithm to convert plain English text to morse code is straightforward. Let’s check the algorithm. Try to write the code in Python. Don’t worry, even if you are not able to write it completely. Let’s check the code for encoding plain English text into morse code. You can see the morse code output below. You should also see a similar morse code in your terminal if you didn’t change the message. Hurray! we got the morse code. You know what comes after. Before jumping into the decoding program, let’s stop for a while and think about how to write code to decode it. … You should have thought about reversing the CHARS_TO_MORSE_CODE_MAPPING dictionary as one of the steps. Doing it manually is hectic and needs to update whenever the original mapping is changed. Let’s write code to reverse the dictionary. We are just reversing the key-value pairs of the given dictionary with the above code. The resultant dictionary will contain values are new keys and keys as new values. We have all the pieces to decode the morse code into plain English text. Without further ado, let’s decode the morse code.

Morse code to English

We can reverse the process of morse code encoding to get the decoding algorithm. Let’s see the algorithm for decoding the morse code into plain English text. Let’s check the code for the above algorithm. I have given the morse code that is generated from the encoding function. We will get the following output if we run the above program. Note: the output is in the English uppercase alphabet because we have used the uppercase alphabet for mapping in the dictionary.

Final Words

We have seen that the output of the decoding function is in uppercase. You can improve the program by making the output as it is in the given time by tracking the lower and upper cases of the English alphabet. This is not related to morse code, as both upper and lower cases have the same pattern. Try it, as it’s more fun to code. That’s it for this tutorial. Use the programs we have written when you encounter morse code next time. Happy Coding 👨‍💻 You may also look at how to create a random password in Python.

How to Create a Morse Code Translator using Python - 39How to Create a Morse Code Translator using Python - 71How to Create a Morse Code Translator using Python - 5How to Create a Morse Code Translator using Python - 79How to Create a Morse Code Translator using Python - 70How to Create a Morse Code Translator using Python - 85How to Create a Morse Code Translator using Python - 28How to Create a Morse Code Translator using Python - 76How to Create a Morse Code Translator using Python - 48How to Create a Morse Code Translator using Python - 23How to Create a Morse Code Translator using Python - 55How to Create a Morse Code Translator using Python - 36How to Create a Morse Code Translator using Python - 93How to Create a Morse Code Translator using Python - 18How to Create a Morse Code Translator using Python - 28How to Create a Morse Code Translator using Python - 11How to Create a Morse Code Translator using Python - 88How to Create a Morse Code Translator using Python - 53How to Create a Morse Code Translator using Python - 37How to Create a Morse Code Translator using Python - 10How to Create a Morse Code Translator using Python - 34How to Create a Morse Code Translator using Python - 56How to Create a Morse Code Translator using Python - 45How to Create a Morse Code Translator using Python - 8