How to Write a Morse Code Translator in Python
I completely understand how difficult it is to use the dictionary, loops, and string operations in Python while making a Morse code converter, especially when you’re a beginner, as different syntax errors and logic issues not only make the process frustrating but also prevent you from building a fully functional translator that can accurately encode and decode messages.
No need to worry, as you’ve stepped onto the right article. In this article, we’ve explored all the nitty-gritty of Morse code in Python so you can clearly understand how it works. We’ve provided a complete Python code of creating encoding and decoding tools that not only translate text to Morse code but also Morse code to text. Whether you want to use it for learning or for strengthening your programming skills, this guide is your cream of the crop.

What is Morse Code?
Morse code is a telecommunication method that encodes text characters into standardized sequences of two different signals of varying duration known as dots and dashes or dits and dahs. Samuel Morse invented it in the 1830s, who is also one of the inventors of the telegraph. Two systems of Morse code are recognized globally: the American and International systems. You can easily memorize and transmit the code as electrical pulses through sound waves and light.
Morse code is straightforward to learn, and you can understand it within a month with just consistent practice. Furthermore, you can also explore a detailed guide on how to learn Morse code. Initially, it was developed for the English language, but was later adapted for use with several other languages, including Arabic, Hebrew, and Japanese. It’s a reliable form of communication as it requires less broadcasting power than voice messages.
How Morse Code in Python Works?
Morse code Python works by representing each letter, number, symbol, and punctuation mark with a distinct sequence of long and short signals, known as dots and dashes. The length of each signal and the pause between them are unique and standardized to send the message effectively. For instance, A is represented with a single dot followed by a single dash (.−), while B is represented with a single dash followed by three dots (−…). Moreover, you can use our Morse code translator tool to understand this communication efficiently.

How to Make a Morse Code Translator in Python
To make a Morse code Translator with Python, you’ll need to implement the following three core functionalities:
- Define a mapping of characters.
- Encoding messages in Morse code.
- Decoding messages from a Morse code sequence.

Defining the Mapping of Characters
The Morse code dictionary contains the following properties:
- The combination of different dots (.) and dashes (-) in Morse code represents each letter, number, punctuation mark, and symbol.
- The forward slash (/) symbol is used as a separator between two words.
We’ve provided a complete dictionary that defines letters, numbers, and symbols in their respective Morse code sequence.
morse_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': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '.': '.-.-.-', ',': '--..--',
'?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...',
':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.',
'$': '...-..-', '@': '.--.-.', ' ': '/'
}Encoding a Message in Morse Code in Python
We’ve provided a function below that takes a string message as input and outputs a Morse code sequence for that message. For instance, the below code converts the Hello World message to (…. . .-.. .-.. — / .– — .-. .-.. -.. -.-.–). Keep in mind that there is a space to separate characters and a forward slash to separate words in the Morse code sequence.
# Define the Morse code dictionary.
morse_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': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '.': '.-.-.-', ',': '--..--',
'?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...',
':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.',
'$': '...-..-', '@': '.--.-.', ' ': '/'
}
# Convert a message to Morse code.
def to_morse_code(message):
morse_code = ''
for char in message.upper():
if char in morse_dict:
morse_code += morse_dict[char] + ' '
return morse_code
# Driver code
def main():
message = "Hello World"
print("Message: ", message, "\nMorse code sequence: ", to_morse_code(message))
if __name__ == "__main__":
main()The above function contains the following properties:
- The function starts with a morse_code variable with an empty string. This variable stores the Morse code sequences.
- The message can be displayed in uppercase, as Morse code is not case-sensitive.
- We go through each character of the capitalized message.
- The function uses each character as a key in the morse_dict dictionary to access its corresponding value. Next, it adds the value and a space character to the morse_code variable.
- Once the message is traversed, the complete Morse code sequence is stored in the morse_code variable. Then the output is given to you.
Decoding a Message from Morse Code Sequence
The function provided below takes Morse code sequences, such as a string of dots, dashes, spaces, and slashes, as input and returns the corresponding messages as output. For instance, this Morse code sequence (.. / .- — / .- / -… — -.– .-.-.-) will be converted to “I AM A BOY”.
# Define the Morse code dictionary.
morse_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': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '.': '.-.-.-', ',': '--..--',
'?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...',
':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.',
'$': '...-..-', '@': '.--.-.', ' ': '/'
}
# Convert a Morse code sequence to a message.
def from_morse_code(morse_code):
message = ''
morse_code = morse_code.split(' ')
for code in morse_code:
for char, morse in morse_dict.items():
if morse == code:
message += char
return message
def main():
morse_code_sequence = ".. / .- -- / .- / -... --- -.-- .-.-.-"
print("Morse code sequence: ", morse_code_sequence, "\nMessage: ", from_morse_code(morse_code_sequence))
if __name__ == "__main__":
main()The above function contains the following properties:
- The Morse code in Python function begins with a message variable set to an empty string. This variable stores the decoded message from the Morse code sequences.
- Then the function resets the morse_code variable by using the split method with a space (” “) as the separator. It’ll break down the Morse code sequence into a list that features the Morse code for each character.
- The function goes through each element of the morse_code list.
- For each item, the function explores the morse_dict dictionary to find the key that matches its Morse code representation. Then, this key will be added to the message variable.
- Once the list is traversed, the message variable stores the decoded text. Then, the output is given to you.
Implementing all Together
We’ve provided the complete Python code below, which not only allows users to translate text to Morse code but also enables them to convert Morse code to text. Here is the complete code:
# Define the Morse code dictionary.
morse_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': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '.': '.-.-.-', ',': '--..--',
'?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...',
':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.',
'$': '...-..-', '@': '.--.-.', ' ': '/'
}
# Convert a message to Morse code.
def to_morse_code(message):
morse_code = ''
for char in message.upper():
if char in morse_dict:
morse_code += morse_dict[char] + ' '
return morse_code
# Convert a Morse code sequence to a message.
def from_morse_code(morse_code):
message = ''
morse_code = morse_code.split(' ')
for code in morse_code:
for char, morse in morse_dict.items():
if morse == code:
message += char
return message
# User interface
def main():
while True:
choice = input("Choose an option:\nPress 1 to convert text to Morse code\nPress 2 to convert Morse code to text\nPress -1 to quit\n")
if choice == '1':
message = input("Enter a message to convert to Morse code: ")
morse_code = to_morse_code(message)
print(morse_code)
elif choice == '2':
morse_code = input("Enter a Morse code sequence to convert to text: ")
message = from_morse_code(morse_code)
print(message)
elif choice == '-1':
print("Quitted successfully")
break
else:
print("Invalid choice, please choose from the available options.")
if __name__ == "__main__":
main()FAQs
Conclusion
As we reach the end of the article, I can confidently say that now you’re well-equipped with everything you need to know about Morse code in Python. We’ve provided a complete code example for creating, encoding, and decoding Morse code using Python, along with practical examples to help you implement it smoothly. You can now easily build your own projects using these insights and bring Morse code into practical use.
