How can I turn a string (like 'hello') into a list (like [h,e,l,l,o])?
            Asked
            
        
        
            Active
            
        
            Viewed 2.3e+01k times
        
    25
            
            
         
    
    
        Jeremy
        
- 1
- 85
- 340
- 366
 
    
    
        Alex Millar
        
- 412
- 2
- 5
- 10
- 
                    5Note that the list will be of strings, `['h', 'e', 'l', 'l', 'o']`. – nmichaels Sep 22 '11 at 23:11
- 
                    8Strings in Python behave like lists of characters. E.g. `'hello'[1]` -> `'e'`. Are you sure you need a list? – Peter Graham Sep 22 '11 at 23:43
- 
                    @PeterGraham: Good point, I've added some description of that to my answer. – Jeremy Sep 22 '11 at 23:49
1 Answers
47
            
            
        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
 
    
    
        Jeremy
        
- 1
- 85
- 340
- 366