String Methods and Transformations
A sequence is an ordered set of values identified by integer indices starting at 0; strings are sequences of characters.
From Text to Positions
A string is more than text displayed on a screen. It is a sequence: an ordered set of values. In a string, the values are characters, and each character has an integer index beginning at 0. This structure lets you select one character, select a range of characters, move through the text, or search for a target.
The vocabulary matters: an index identifies one item, a slice selects a contiguous range, and traversal visits items systematically.
Reading Character Positions
Consider the string hello. It contains five characters arranged in order. The first character is at index 0, the second at index 1, and so on. An index selects a single item from the sequence, so an index gives you one character rather than a range.
Selecting One Character
Use the sequence hello to reason about the character at index 2.
Identify the index: Index 2 is the third position because indexing begins at 0.
Read the item: The character arranged at that position is l.
An index selects one character from a string.
Selecting Contiguous Ranges
A slice selects a contiguous range instead of one character. It has a start index and an end index. The start is included, while the end is excluded. Therefore, a slice from index 1 to index 4 selects the characters at indices 1, 2, and 3. In hello, those characters are ell; the character at index 4 is not part of the slice.
Methods and New Strings
Strings have methods that perform operations such as converting text to uppercase, finding the position of a character, or replacing one substring with another. A method is invoked by writing the string or a variable that holds it, then a dot, then the method name with parentheses. The notation connects the operation to the specific string object on which it should be performed.
Strings are immutable. That means an individual character in the original string cannot be changed in place. Immutability does not prevent string processing. Instead, concatenation or a string method can create a new string, while the original remains unchanged.
Reasoning About a Transformation
Suppose a method is used to convert a string to uppercase. What should you expect about the original string and the transformed result?
Start with the original: The original string cannot have one of its characters replaced in place because strings are immutable.
Invoke the method: Use dot notation to invoke the uppercase-conversion method on the string object.
Track the result: The operation creates a new string containing the transformed text. The original string remains unchanged.
A string transformation produces a new string rather than editing an individual character in the original.
Moving Through Every Character
Traversal means visiting every item in a sequence systematically. For a string, traversal moves through its characters in order. This pattern is useful when you need to examine every character, count occurrences, or apply a transformation to all items.
Counters and flags often support traversal. A counter records how many matching characters have been encountered. A flag records whether a search condition has been satisfied. These variables help preserve information gathered while the traversal progresses.
Stopping When a Match Appears
Search is a specialized traversal. The process examines characters one at a time and checks each against a target character or substring. When the target is found, the search stops and reports the result. If the target is not found, the search reaches the end without a match.
A search can avoid unnecessary work when the target appears early because it stops at the first match. A flag can preserve the information that a match was found, while an index can identify where the match occurred.
Empty Strings and Boundaries
The empty string is a valid string value. It has length 0 and contains no items. It can appear when a user provides no input or when a search produces an empty result. Treating it as a legitimate value helps you distinguish an empty result from an error or undefined state.
Mistakes to Avoid
Treating the first character as index 1.
String indices begin at 0, so index 1 identifies the second character.
Fix:
Count the first character as index 0 and increase the index by one for each later position.Including the end index in a slice.
A slice includes its start index but excludes its end index.
Fix:
Select indices 1, 2, and 3 for a slice whose boundaries are 1 and 4.Trying to replace one character in place.
Strings are immutable, so individual characters cannot be changed in place.
Fix:
Create a new string through concatenation or a string method.Confusing traversal with search.
Search is a specialized traversal that stops when the target is found.
Fix:
Stop at the match when the task only requires finding whether or where the target occurs.Forgetting the object in dot notation.
Dot notation invokes the method on a specific string object.
Fix:
Identify the string or string-holding variable, then the dot, method name, and parentheses.
Practice the Patterns
For the string hello, identify the character at index 3, identify the characters selected by the slice from index 0 to index 2, and describe whether a method that transforms the text changes the original string or creates a new one.
Hints
- Begin counting positions at 0.
- Include the slice start and exclude its end.
- Use immutability to reason about the original string.
Describe the control flow for searching the string hello for the character l. State what happens when the first l is encountered and what a flag could record.
Hints
- Search checks one character at a time.
- The process stops when the target is found.
- A flag can remember whether the target was found.
Key Takeaways
- Strings are sequences of characters arranged in order and identified by integer indices beginning at 0.
- An index selects one item, while a slice selects a contiguous range with an inclusive start and exclusive end.
- Strings are immutable, so transformations create new strings instead of changing individual characters in place.
- Dot notation invokes a method on a particular string object.
- Traversal visits every character systematically, while search is a traversal that stops when a target is found.
- The empty string is a valid string with length 0 and no items.
Key Takeaways
- A string is an ordered sequence of characters whose indices begin at 0.
- Slices use an inclusive start index and an exclusive end index.
- String immutability means operations create new strings rather than changing individual characters in place.
- Methods are invoked with dot notation on a particular string object.
- Traversal visits every item, while search stops when it finds its target.