Morse Code Converter

Convert text to Morse code easily

Text to be converted:


    # Morse code dictionary — maps letters and digits to Morse symbols.
    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": "--..", "1": ".----", "2": "..---", "3": "...--",
        "4": "....-", "5": ".....", "6": "-....", "7": "--...", "8": "---..",
        "9": "----.", "0": "-----", " ": "/",
    }

    def text_to_morse(text):
        # Step 1: Convert all letters to uppercase 
        # (so dictionary lookups match — Morse only uses uppercase keys)
        text = text.upper()

        # Step 2: Loop through each character in 'text'
        # For each one, look up its Morse code in MORSE_DICT.
        # If the character isn’t found (e.g. punctuation), return an empty string "".
        # Step 3: Join all Morse codes into a single string separated by spaces.
        return " ".join(MORSE_DICT.get(c, "") for c in text)

    # Example:
    # text_to_morse("HELLO") → ".... . .-.. .-.. ---"