We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

Morse code is a method to encode a message usingdots,dashes, andspaces.

morse-code

Its widely used to communicate messages secretly.

You might have seen morse code usage in navy scenes of many movies to communicate messages.

you’re free to referWikipedia pageof 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.

One thing to remember is that both uppercase and lowercase alphabets have the same morse code pattern.

Lets 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.

Lets check the algorithm.

give a shot to write the code in Python.

Dont worry, even if you are not able to write it completely.

Lets check the code for encoding plain English text into morse code.

you’ve got the option to see the morse code output below.

You should also see a similar morse code in your terminal if you didnt change the message.

we got the morse code.

You know what comes after.

You should have thought about reversing theCHARS_TO_MORSE_CODE_MAPPINGdictionary as one of the steps.

Doing it manually is hectic and needs to update whenever the original mapping is changed.

Lets 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, lets decode the morse code.

Lets see the algorithm for decoding the morse code into plain English text.

Lets 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 initiate the above program.

Final Words

We have seen that the output of the decoding function is in uppercase.

This is not related to morse code, as both upper and lower cases have the same pattern.

Try it, as its more fun to code.

Thats it for this tutorial.

Use the programs we have written when you encounter morse code next time.

Happy Coding

You may now look at how tocreate a random password in Python.

Also, check these English to Morse code translators.