Split Text into Sentences in Python
Learn how to split text into sentences in Python, compare simple punctuation approaches with NLP-aware tokenizers, and handle practical edge cases.
What splitting text into sentences means
Splitting text into sentences means taking a longer piece of text and separating it into sentence-sized units. The input is usually one string, the intended output is normally a list of sentence strings, and the practical purpose may be summarization, analysis, translation preparation, or another task where sentence boundaries matter.
It is useful to distinguish this from visual line wrapping. Sentence splitting changes the underlying text by inserting boundaries between sentence units. Line wrapping only changes how text is displayed without altering the text itself.
Simple punctuation-based splitting
A basic approach is to look for sentence-ending punctuation such as periods, question marks, and exclamation marks. This can work for simple text, but ordinary punctuation alone is not a reliable sentence boundary signal. Periods appear in abbreviations, decimal numbers, initials, URLs, and other non-final positions.
import re
text = "Hello world. How are you? I am fine."
sentences = re.split(r'(?<=[.!?])\s+', text)
print(sentences)
# ['Hello world.', 'How are you?', 'I am fine.']
This example is illustrative rather than a general solution. It separates text after punctuation that is followed by whitespace, but it does not understand whether the punctuation actually ends a sentence.
NLP-aware sentence tokenizers
A more robust method is to use a sentence tokenizer designed to recognize sentence boundaries. One well-known option is the Punkt sentence tokenizer available through the Natural Language Toolkit, often called NLTK. It uses language-specific models and can handle many common cases better than a plain regular expression.
from nltk.tokenize import sent_tokenize
text = "Dr. Smith arrived at 10 a.m. He left at noon."
sentences = sent_tokenize(text)
print(sentences)
# ['Dr. Smith arrived at 10 a.m.', 'He left at noon.']
This example assumes the required NLTK data is installed. The tokenizer may still vary in accuracy depending on the text, language, and formatting.
Other structured text handling options
For structured text, fixed-length segmentation can divide text into chunks of a constant character or token length. This is useful when sentence boundaries are less important than consistent chunk size, such as when preparing input for a model with a limited context window. Fixed-length splitting may cut inside a sentence unless it is combined with boundary handling.
Delimiter-based separation is another option when the text already contains known markers, such as line breaks, tabs, or custom separators. If a document already uses a clear record separator, splitting on that delimiter can be more predictable than trying to infer sentence boundaries.
Practical constraints to consider
- Punctuation alone may fail on abbreviations, decimals, ellipses, and quoted speech.
- Tokenizer performance may depend on language-specific data and the style of the input.
- Sentence splitting changes the underlying text; visual wrapping does not.
- Fixed-length splitting is not the same as sentence splitting, but it can be useful when the output unit is defined by size rather than meaning.
Choosing an approach
Choose a punctuation-based split for simple, predictable text and when a lightweight solution is enough. Choose an NLP-aware tokenizer when abbreviations, names, decimal numbers, or other ambiguous periods are likely. Use delimiter-based splitting when the source text already contains known boundaries, and use fixed-length segmentation when sentence meaning is less important than consistent chunk size.
The supplied source does not provide measured accuracy comparisons between these methods. Treat code examples as starting points and verify results on representative input before relying on any approach.