python - How can I make a list from the string -
is there way make string:
"i python!!!"
a list like
['i', 'l', 'i', 'k', 'e', 'p', 'y', 't', 'h', 'o', 'n', '!', '!', '!']
use list comprehension:
>>> mystr = "i python!!!" >>> [c c in mystr if c != " "] ['i', 'l', 'i', 'k', 'e', 'p', 'y', 't', 'h', 'o', 'n', '!', '!', '!'] >>> [c c in mystr if not c.isspace()] # alternately ['i', 'l', 'i', 'k', 'e', 'p', 'y', 't', 'h', 'o', 'n', '!', '!', '!'] >>>
Comments
Post a Comment