I have string as:
myString = 'example'
How can I convert it into a list as :
lst = ['example']
in an efficient way?
shahidfoy
2,3512 gold badges19 silver badges23 bronze badges
asked Jan 28, 2019 at 5:37
sunil shrestha
474 silver badges11 bronze badges
3 Answers 3
The most natural way is correct:
mystr = 'example'
lst = [mystr]
Also, don't name your variables str; it overrides the built-in str.
answered Jan 28, 2019 at 5:38
iz_
16.8k4 gold badges29 silver badges43 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
you can use the append() function to achieve that:
lst = []
str = 'example'
lst.append(str)
answered Jan 28, 2019 at 5:41
syazwanSahdom
111 silver badge3 bronze badges
Comments
str='example'
l=list(str)
Now l will contain ['e', 'x', 'a', 'm', 'p', 'l', 'e']
Comments
lang-py
[s]ors.split()whens='example'. Althoughs.split()is much slower.