Turning a Python String Into a List


#1

Hi,

This is not really related to any Code Academy exercises (or might be).

If I have a string such as a = 'hello my name is max', how would I turn it into a list that looks like lst = ['hello my name is max']

Thank you!


#2

well you could do something like:

a = 'hello my name is max'
lst = [a]
print lst

you can simply store the variable into the list, this will store the string in the list. Or if you want you can also first declare the list:

lst = []

and then use the build in append() function to append to the list:

lst.append(a)

i would choice the first approach


#3

I just want to ask a question that's not completely related, but is append similar to the push method in JS?


#4

apart from the fact that under the hood they are implanted different (since it are different languages), yes, they both add (or rather append) items to a list/array.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.