-4

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
2
  • either [s] or s.split() when s='example'. Although s.split() is much slower. Commented Jan 28, 2019 at 5:52
  • 1
    Possible duplicate of String to list in Python Commented Jan 28, 2019 at 5:53

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

you can use the append() function to achieve that:

lst = []
str = 'example'
lst.append(str)
answered Jan 28, 2019 at 5:41

Comments

1

str='example'

l=list(str)

Now l will contain ['e', 'x', 'a', 'm', 'p', 'l', 'e']

answered Jan 28, 2019 at 5:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.