I feel this is answer might be simple, but I need some help. I need to create a list of 3 sentences from a docstring of three sentences. I initially thought the split() method could be used, but the problem with using split is that I need to include all parts of each sentence. I need all spaces and punctuation, so my argument in split() can't be a punctuation mark or empty string from my understanding. Could you help me create a list with each element being one sentence? Here the code that I have so far:
sentence = ''
docstring = 'The rain in #Spain in 2019, rained "mainly" on the plain.\
There is a nice function to split a string into a list based on a given \
delimiter! Why do I try to do too much?'
for character in docstring:
sentence += character
if character == '.' or character == '?' or character == '!':
sentencelist.append(sentence)```
docstring = 'The rain in #Spain in 2019, rained "mainly" on the plain. There is a nice function to split a string into a list based on a given delimiter! Why do I try to do too much?'
sentencelist = re.split(r'(?<=[.!?])\s+', docstring)
print(sentencelist)
Instead of going character by character you can simply use regex for the purpose. And to retain those those the result we use positive lookbehind assertion in our regex.
Output:
['The rain in #Spain in 2019, rained "mainly" on the plain.', 'There is a nice function to split a string into a list based on a given delimiter!', 'Why do I try to do too much?']