I can split a sentence into individual words like so:
string = 'This is a string, with words!'
string.split(" ")
['This', 'is', 'a', 'string,', 'with', 'words!']
But I don't know how to split a word into letters:
word = "word"
word.split("")
Throws me an error. Ideally I want it to return ['w','o','r','d'] thats why the split argument is "".
list(word)
you can pass it to
list
In Python string is iterable. This means it supports special protocol.
list
constructor may build list of any iterable. It relies on this special methodnext
and gets letter by letter from string until it encountersStopIteration
.So, the easiest way to make a list of letters from string is to feed it to
list
constructor:You can iterate over each letter in a string like this:
In python send it to