Python Strings and String Methods
In Python, a piece of text is stored as a string.
Why Text Needs Preparing
Many machine learning problems start with numbers in a table. A large share of real data, however, arrives as text: messages, reviews, documents, and notes. Text does not come pre-sorted into numeric columns, so it has to be handled before a learning method can use it. Working with text data is a category of methods that includes text-specific processing techniques for preparing text.
As you read this article, keep one question in mind: what does a piece of text look like to a program before and after a processing step is applied to it? Following that question step by step is the best way to build a clear mental model of the topic. Before studying any specific technique, you need to understand the Python data type that text is stored in and how basic operations on that type work.
How Python Stores Text
In Python, a piece of text is stored as a string. A string is an ordered sequence of characters, and every character has a position, called an index, starting at 0. Because of this ordering, you can ask a string two basic questions: how many characters it contains, and which character sits at a given position.
Take the text Data Science. The space counts as one character, so the length is 12. Index 0 is the letter D, and index 5 is the letter S, because the space occupies index 4. Counting positions this way is the foundation for every operation that follows.
12
D
Slen(text)counts every character, including the space- text[0] reads the character at index 0, which is D
- text[4] is the space, so it prints as a blank
- text[5] is the letter S, the first character after the space
What do you think happens?
If text = "Data Science", what does print(text[9]) show?
Reveal answer
c
Indexes count from 0, so the characters are D(0) a(1) t(2) a(3) space(4) S(5) c(6) i(7) e(8) n(9). Index 9 is the letter n, not c. Count carefully: the answer is n.
Methods That Hand Back New Strings
A text processing step takes a string as input and produces a new value as output. Python string methods, such as lower() and split(), show this pattern clearly. Each method call follows the same shape: a string goes in, and a result comes out. Once you recognize this shape, you can read any string method call as a small transformation with an input and an output.
The method does not reach back and rewrite the string it was called on. It computes a separate value and hands it back. If you want to use that value later, you have to store it, usually in a new variable or by assigning it back to the same name.
text = "Data Science"
text.lower()
print(text)The result of lower() is discarded, so text still holds Data Science.
text = "Data Science"
lowered = text.lower()
print(lowered)The result of lower() is stored in lowered, so the new value can be used.
Chaining Two Methods
The source applies two methods in sequence, which mirrors how a multi-step text workflow is built. In a chain, the output of the first step becomes the input of the second. The intermediate value exists after the first step, even if you never name it, and the final result depends on the order of the steps.
Lowercasing and then splitting a string
Starting from text = "Data Science", build a list of lowercase words using lower() and split().
- 1Start with the original string
text holds the string Data Science, with 12 characters.
- 2Apply lower() to the original
lower()reads text and returns the new value data science. text itself still holds Data Science. - 3Apply split() to the lowercase result
split()takes the lowercase string and returns a list of its words, breaking at the space: data and science. - 4Store the final result
Assigning the result to words keeps the final value for later use.
words = text.lower().split() gives ['data', 'science']
['data', 'science']
Data Sciencetext.lower()runs first and produces data sciencesplit()runs on that new value, not on text- text still holds the original Data Science after the chain
Mistakes With In-Place Assumptions
Common mistake Assuming a string method changes the original variable
What it looks liketext = "Data Science" text.lower() print(text) # prints Data ScienceWhy it failslower()returns a new value. Nothing is assigned back to text, so the original string is unchanged.Do this insteadStore the result: lowered = text.lower(), or text = text.lower() if you want to replace the name's value.
Common mistake Expecting a chained result to be stored automatically
What it looks liketext = "Data Science" text.lower().split() print(text)Why it failsEach step in the chain produces a value that is passed on, but the final value is only kept if you assign it to a name.
Do this insteadWrite words = text.lower().split() so the final list is saved.
Common mistake Confusing the index of a character with the character's position in a word when counting, so index 5 in Data Science is S, not the fifth letter of Data
Try It Yourself
Let text = "Data Science". Without running the code, work out what len(text) returns, what text[0] returns, and what words holds after words = text.lower().split(). Then check your answers in Python.
Hints
- Count the space as one character when finding the length.
- Indexes start at 0, so the first letter is at index 0.
- Remember that
lower()andsplit()return new values, and thatsplit()returns a list.
Key Takeaways
- Text data is a distinct task in machine learning, and text-specific processing techniques are a category of methods for preparing it.
- In Python, a string is an ordered sequence of characters, where each character has an index starting at 0.
- A string method takes a string as input and returns a new value as output, following the same input-to-output shape every time.
- String methods do not change the original string, so their results must be stored to be used.
- Chaining methods passes each step's output on as the next step's input, and the order of the steps shapes the final result.