Morse Code Translation In Python

Morse Code

Have you ever tried encrypting your secret message into certain codes?

Morse code is a form of communication that is represented in-dash and dot pairs. Where dash(-) sounds dah and dot(.) sounds di or dit.

Convert your Favourite Anime Character Name into a Morse code. Yes, it is possible as long as you know Python. But before that what is Morse Code all about?

What is Morse Code?

Morse Code is an encryption-based communication with a series of dot and dash pairs. This was named after Samuel Morse, the inventor of the telegraph. With help of the Morse Code encrypted messages could be sent to large distances through signals. Every alphabet has its unique Morse code. And not just the alphabet, even symbols, and Arabic Numerals has its unique code. The most used alphabet was given the shorter sequences of dots and dashes. For example, the most commonly used letter is E, which is represented as a single dot. And T, which is represented as a single dash.

For what purpose was Morse Code used?

In order to transmit messages across the telegraph wires, Morse created Morse Code. The telegraphic code used for amateur radio telegraphic communication is the International Morse Code consisting of dots and dashes. Morse code was critical for communication during World War II. It was used in the Military for security and alert messages signals. So it makes sense why SOS was very famous during that time. Since “S” represents three dots and three dashes form an “O” in the International Morse code, though, the signal came to be called an “SOS”. SOS is a Morse code distress signal, used internationally, that was originally established for maritime use. Know about SOS.

Morse Code Python

Since every alphabet has its own Morse code, refer to this site and store the Morse Code of each alphabet in a dictionary. Then enter your favorite character name. Using enumerate function, loop through each alphabet and convert that alphabet into the respective Morse Code. This is very simple. Try it out:

import string

#store morse code value to specific alphabet
alpha = string.ascii_uppercase
morse = dict()
for code,letter in enumerate(alpha):

text = input("Enter the Text: ").upper()
#before converting
print("Character Name Before:",text)

#double space means new letter, while single space is gap between two moose converted letter
morse_converted = ""
for letter in text:
    if letter==" ":
        morse_converted+="  "
    if letter in morse:
        morse_converted+=morse[letter]+" "

#after converting
print("Character Name In morse:",morse_converted)

Enumerate Python

If you notice the code you have used a function named Enumerate. Before knowing Enumerate let’s look into an example. Consider a List and print the elements of the list with their index number.

Also Read: Anime Hangman

>>> myanimelist = ["One Piece","Gintama","Attack On Titan","Code Geass","Monster"]
>>> for i in range(len(myanimelist)):
...  print(i+1,myanimelist[i])
1 One Piece
2 Gintama
3 Attack On Titan
4 Code Geass
5 Monster

The same code lets code it with enumerate.

>>> for index,anime in enumerate(myanimelist,1):
...  print("{}:{}".format(index,anime))
1:One Piece
3:Attack On Titan
4:Code Geass


Enumerate makes our job easy by taking both index and value in one go. Enumerate takes two arguments, the first argument consists of sequence and the second argument denotes the starting index value. By default, the starting index is 0.


Morse Code has a long history. It is even used today. There is so much about it which was not included in this article. If you are interested to know more about Morse Code. Check out this article on Morse Code at Daily Dabble.

If you like the content, support us by Buying a Coffee.

Enter your favorite character name or your own name and check its Morse Code Translation.