22

say I have the following string:

"Hello there. My name is Fred. I am 25.5 years old."

I want to split this into sentences, so that I have the following list:

["Hello there", "My name is Fred", "I am 25.5 years old"]

As you can see, I want to split the string on all occurrences of the string ". ", not any occurrence of either "." or " ". Python's str.split() will not work in this case because it will treat each character of the string as a separate delimiter, rather than the whole string as a multi-character delimiter. Is there a simple way to solve this problem?

1
  • 2
    split does not behave like strip in that regard. Commented Nov 10, 2011 at 15:07

3 Answers 3

38

Works for me

>>> "Hello there. My name is Fr.ed. I am 25.5 years old.".split(". ")
['Hello there', 'My name is Fr.ed', 'I am 25.5 years old.']
Sign up to request clarification or add additional context in comments.

1 Comment

That's a better solution than using regular expressions!
4
>>> "Hello there. My name is Fred. I am 25.5 years old.".rstrip(".").split(". ")
['Hello there', 'My name is Fred', 'I am 25.5 years old']

Comments

3

You can use split function in regular expression library :

import re
re.split('\. ', "Hello there. My name is Fred. I am 25.5 years old.")

1 Comment

Thanks for the alternative suggestion. This is helpful if I have multiple delimiters. re.split(r'[\s,.|/]+', 'The.\tbrown fox|jumped,over/the') returns ['The', 'brown', 'fox', 'jumped', 'over', 'the']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.