How do I split a string into individual words and store those words onto an array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I split a string into individual words and store those words onto an array?

18th Feb 2017, 6:35 PM
Mohammad Jawarneh
Mohammad Jawarneh - avatar
2 Answers
+ 2
You can split a string into a list ( array's name in Python ), using a separator character: "thing,trick,stuff".split(",") # output: [ "thing", "trick", "stuff" ] If you have sentences usually space is used as separator of words: words = "A sentence, usually, is composed of words...".split(" ") print(word[1]) ... wil output "sentence," so you need maybe to do a treatment to your collected words if you don't want punctuation or whatever... Another point: the list generated could contains many times the same word: if you want a list of unique words, transform your splitted text ( the word list ), into a set ( and as is immutable -- and unordered -- transform it again in list if you need to modify it ): uniques_words = set(words) new_editable_list = list(uniques_words)
19th Feb 2017, 7:34 AM
visph
visph - avatar
+ 1
I like this question. I've been having the problem with the same. I've tried something like this string a; a.Substring(); or a.Remove but I'm still trying to find a better way to deal with it
18th Feb 2017, 7:37 PM
Tuchy
Tuchy - avatar