Strings: Text Processing at the Lowest Level

It’s time to study a fundamental data type that we’ve been studiously avoiding so far. In earlier chapters we focused on a text as a list of words. We didn’t look too closely at words and how they are handled in the programming language. By using NLTK’s corpus interface we were able to ignore the files that these texts had come from. The contents of a word, and of a file, are represented by programming languages as a fundamental data type known as a string. In this section, we explore strings in detail, and show the connection between strings, words, texts, and files.

Basic Operations with Strings

Strings are specified using single quotes 1 or double quotes 2, as shown in the following code example. If a string contains a single quote, we must backslash-escape the quote 3 so Python knows a literal quote character is intended, or else put the string in double quotes 2. Otherwise, the quote inside the string 4 will be interpreted as a close quote, and the ...

Get Natural Language Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.