fork download
  1. data = 'Hi, Welcome to MK training. I love python.'
  2.  
  3. words = []
  4. word = ''
  5.  
  6. # loop through each character
  7. for x in data:
  8.  
  9. print('Processing ' + x)
  10.  
  11. # if character is a separator and if word variable has something in it
  12. # then store it in words list
  13. if x in [',', '.', ' ']:
  14.  
  15. print('!! Found a separator !!')
  16.  
  17. if word:
  18. print(f'Adding the word "{word}" to {words}. Resetting word.')
  19. words.append(word)
  20. word = ''
  21. print(f'Words is now {words}')
  22.  
  23. else:
  24. word += x
  25. print(f'Word is {word}')
  26.  
  27. # after looping through all characters, if there's still something
  28. # in word, store it in words list
  29. if word:
  30. words.append(word)
  31.  
  32. print(words)
  33.  
Success #stdin #stdout 0.03s 9156KB
stdin
Standard input is empty
stdout
Processing H
Word is H
Processing i
Word is Hi
Processing ,
!! Found a separator !!
Adding the word "Hi" to []. Resetting word.
Words is now ['Hi']
Processing  
!! Found a separator !!
Processing W
Word is W
Processing e
Word is We
Processing l
Word is Wel
Processing c
Word is Welc
Processing o
Word is Welco
Processing m
Word is Welcom
Processing e
Word is Welcome
Processing  
!! Found a separator !!
Adding the word "Welcome" to ['Hi']. Resetting word.
Words is now ['Hi', 'Welcome']
Processing t
Word is t
Processing o
Word is to
Processing  
!! Found a separator !!
Adding the word "to" to ['Hi', 'Welcome']. Resetting word.
Words is now ['Hi', 'Welcome', 'to']
Processing M
Word is M
Processing K
Word is MK
Processing  
!! Found a separator !!
Adding the word "MK" to ['Hi', 'Welcome', 'to']. Resetting word.
Words is now ['Hi', 'Welcome', 'to', 'MK']
Processing t
Word is t
Processing r
Word is tr
Processing a
Word is tra
Processing i
Word is trai
Processing n
Word is train
Processing i
Word is traini
Processing n
Word is trainin
Processing g
Word is training
Processing .
!! Found a separator !!
Adding the word "training" to ['Hi', 'Welcome', 'to', 'MK']. Resetting word.
Words is now ['Hi', 'Welcome', 'to', 'MK', 'training']
Processing  
!! Found a separator !!
Processing I
Word is I
Processing  
!! Found a separator !!
Adding the word "I" to ['Hi', 'Welcome', 'to', 'MK', 'training']. Resetting word.
Words is now ['Hi', 'Welcome', 'to', 'MK', 'training', 'I']
Processing l
Word is l
Processing o
Word is lo
Processing v
Word is lov
Processing e
Word is love
Processing  
!! Found a separator !!
Adding the word "love" to ['Hi', 'Welcome', 'to', 'MK', 'training', 'I']. Resetting word.
Words is now ['Hi', 'Welcome', 'to', 'MK', 'training', 'I', 'love']
Processing p
Word is p
Processing y
Word is py
Processing t
Word is pyt
Processing h
Word is pyth
Processing o
Word is pytho
Processing n
Word is python
Processing .
!! Found a separator !!
Adding the word "python" to ['Hi', 'Welcome', 'to', 'MK', 'training', 'I', 'love']. Resetting word.
Words is now ['Hi', 'Welcome', 'to', 'MK', 'training', 'I', 'love', 'python']
['Hi', 'Welcome', 'to', 'MK', 'training', 'I', 'love', 'python']