0

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?

Jacob_
  • 247
  • 2
  • 13
  • What do you mean by it doesn't work? Are you getting any errors? Or it is giving wrong output? – thefourtheye Jul 05 '16 at 10:03
  • @jonrsharpe OP has given the code which he has tried, I think he just needs a little help to get it fixed. Should this be closed as duplicate? – thefourtheye Jul 05 '16 at 10:04
  • @thefourtheye that answers *"is there any way to do this? Perhaps an inbuilt function or something?"* at least. I assumed the OP wanted to solve their problem rather than fix this specific code. – jonrsharpe Jul 05 '16 at 10:05
  • @jonrsharpe I didn't realise this was a duplicate - I did look. Could you send a link to the question which is identical to this? – Jacob_ Jul 05 '16 at 10:09
  • @Jacob_ ...it's in the large banner at the top of the question ^^. Also under **Linked** in the sidebar >> – jonrsharpe Jul 05 '16 at 10:48
  • @jonrsharpe yep just saw it actually – Jacob_ Jul 05 '16 at 11:28

0 Answers0