String to list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

String to list

Please is there anyway to change a string to a list?? E.g. file=("this is a sentence") so my question is that is there anyway to change file into a list with 'this' having index 0, 'is' with index 1 , 'a' with index 2 and 'sentence' with index 3

29th Sep 2018, 2:09 PM
‏‪‏‪Sammy_Jamez‬‏‬‏
‏‪‏‪Sammy_Jamez‬‏‬‏ - avatar
4 Answers
+ 4
Yes, there is. You must use split like this: file = "this is text" l = file.split(" ") print(l) It will bring you what you want.
29th Sep 2018, 2:44 PM
Enzo Falcon
Enzo Falcon - avatar
+ 3
if you have simple string without spaces like this. x='man' you can convert into list in very simple way using list function. print(list(x)) #output=['m','a','n']
29th Sep 2018, 4:07 PM
Maninder $ingh
Maninder $ingh - avatar
+ 2
def func(a): print(a.split(" ")) func("this is some help")
29th Sep 2018, 4:15 PM
Sousou
Sousou - avatar
0
sorry but does it work for an integer and how?
29th Sep 2018, 9:37 PM
‏‪‏‪Sammy_Jamez‬‏‬‏
‏‪‏‪Sammy_Jamez‬‏‬‏ - avatar