Search code examples
pythonmicropythontext-segmentationbbc-microbit

Split user input string into a list with every character


I'm trying to write a program for the micro:bit which displays text as morse code. I've looked at multiple websites and Stack Overflow posts for a way to split a string into characters.

E.g. string = "hello" to chars = ["h","e","l","l","o"]

I tried creating a function called array, to do this, but this didn't work.

I then tried this:

def getMessage():
    file = open("file.txt", "r")
    data = file.readlines() 
    file.close()
    words = []
    for line in data:
        for word in line:
            words.append(word)
    return words

Any ideas?


Solution

  • You can use builtin list() function:

    >>> list("A string") 
    ['A', ' ', 's', 't', 'r', 'i', 'n', 'g'] 
    

    In your case, you can call list(getMessage()) to convert the contents of the file to chars.