+ 3

[python] Splitting a string

Hi, I am bit new to python. I want to split a string something like this: x='print "Thank you"' #Some code The output should be: ['print', '"Thank you"'] as you can see it did not split Thank you inside " " How can I do that?? And Thanks is advance.

14th Oct 2018, 4:17 AM
LoystonLive
LoystonLive - avatar
1 Answer
0
You have to define your own text analyzer. lesson: https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2457/ Solution:  def splitText(txt):     wordList = ['']     wordCount = 0     for i in range(len(txt)):        if txt[i]==' ' or txt[i]=='"':           wordCount += 1           wordList.append('')        else:           wordList[wordCount]+=txt[i]     return wordList     print(*splitText(x)) Already coded for you: https://code.sololearn.com/cT73x1s4e3xs/?ref=app You can add more filter criteria in the if statement.😎
14th Oct 2018, 8:46 AM
Gordon
Gordon - avatar