2

I would like to split a string into separate sentences in a list.

example:

string = "Hey! How are you today? I am fine."

output should be:

["Hey!", "How are you today?", "I am fine."]

vimuth
  • 5,064
  • 33
  • 79
  • 116
rose
  • 31
  • 5

4 Answers4

2

You can use a built-in regular expression library.

import re
string = "Hey! How are you today? I am fine."
output = re.findall(".*?[.!\?]", string)

output>> ['Hey!', ' How are you today?', ' I am fine.'] 

Update:

You may use split() method but it'll not return the character used for splitting.

import re
string = "Hey! How are you today? I am fine."
output = re.split("!|?", string)
output>> ['Hey', ' How are you today', ' I am fine.']

If this works for you, you can use replace() and split().

string = "Hey! How are you today? I am fine."
output = string.replace("!", "?").split("?")
codeezer
  • 170
  • 8
0

You can use the methode split()

import re
string = "Hey! How are you today? I am fine."
yourlist = re.split("!|?",string)
Karam Mohamed
  • 843
  • 1
  • 7
  • 15
0

you can try

>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']

I find it in here

ngo huy
  • 26
  • 6
0

You don't need regex for this. Just create your own generator:

def split_punc(text):
    punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    # Alternatively, can use:
    # from string import punctuation
    j = 0
    for i, x in enumerate(text):
        if x in punctuation:
            yield text[j:i+1]
            j = i + 1
    return text[j:i+1]

Usage:

list(split_punc(string))
# ['Hey!', ' How are you today?', ' I am fine.']
r.ook
  • 13,466
  • 2
  • 22
  • 39