How Can I Turn a String into a List in Python? [Duplicate]
How Can I Turn a String (Like 'Hello') into a List (Like [H, E, L, L, O])? 3 1 Answer the List() Function [Docs] Will Convert a String into a List of...
How can I turn a string (like 'hello') into a list (like [h,e,l,l,o])?
1 Answer
The list() function [docs] will convert a string into a list of single-character strings.
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
Even without converting them to lists, strings already behave like lists in several ways. For example, you can access individual characters (as single-character strings) using brackets:
>>> s = "hello"
>>> s[1]
'e'
>>> s[4]
'o'
You can also loop over the characters in the string as you can loop over the elements of a list:
>>> for c in 'hello':
... print c + c,
...
hh ee ll ll oo