I'm not sure if the title was descriptive enough, so essentially what I want to do is, say I have this string:
a = "Hello, world! This...is my string."
And I want to seperate this into a list of each of it's words, but I want the punctuation to be counted as seperate words. So, like this:
["Hello", ",", " ", "world", "!", " "....
and so on...
Notice that each space is also a seperate word.
Here's the code which I've tried:
arr = "hello this, is my, st.ring! I, will split it."
final = []
buffer = ''
delims = list(',.! ')
print(delims)
for i in arr.split():
for a in i:
buffer = buffer + a
try:
if a in delims:
final.append(buffer)
buffer = ''
except IndexError:
pass
# if no punctuation in the word
final.append(buffer)
buffer = ''
for i in final:
print(i)
Basically this code iterates the string and has a buffer, which is the previously checked characters. When a character is found which is in delims
, the buffer is cleared after adding it's contents to a list.
But it doesn't work. I'm not really sure why, but is there any way to do this? Perhaps an inbuilt function or something?