Concepts / String Methods and Transformations

String Methods and Transformations

A sequence is an ordered set of values identified by integer indices starting at 0; strings are sequences of characters.

  • Programming

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.

Key point

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.

0h1e2l3l4onextnextnextnext
How does each character in a string map to an integer index starting at 0?
Worked example

Selecting One Character

Use the sequence hello to reason about the character at index 2.

  1. 1
    Identify the index

    Index 2 is the third position because indexing begins at 0.

  2. 2
    Read the item

    The character arranged at that position is l.

Result

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.

0h1e2l3l4onextnextnextend boundary
Which range of character indices is selected when a slice starts at 1 and ends at 4?

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.

textstring object.connects objectmethodoperation name()invokes methodthenthenthen
How does a string object connect to the method being called through dot notation?

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.

original textunchangedmethod operationcreates a stringnew texttransformed resultused byreturns
What changes when a string operation transforms text, and does it modify the original string?
Worked example

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?

  1. 1
    Start with the original

    The original string cannot have one of its characters replaced in place because strings are immutable.

  2. 2
    Invoke the method

    Use dot notation to invoke the uppercase-conversion method on the string object.

  3. 3
    Track the result

    The operation creates a new string containing the transformed text. The original string remains unchanged.

Result

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.

hindex 0eindex 1lindex 2lindex 3oindex 4endall items visitednextnextnextnextfinish
How does control move from one character to the next while visiting every character?

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.

next characterinspecttarget matchcheckfoundstopnext positioncontinueend of stringnot foundinspectyesnocontinueno positions remain
How does a search inspect a string and determine whether a target is present?

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

  • Common mistake

    Treating the first character as index 1.

    What it looks like

    Assigning the first position the number 1.

    Why it fails

    String indices begin at 0, so index 1 identifies the second character.

    Do this instead

    Count the first character as index 0 and increase the index by one for each later position.

  • Common mistake

    Including the end index in a slice.

    What it looks like

    Expecting a slice from index 1 to index 4 to include the character at index 4.

    Why it fails

    A slice includes its start index but excludes its end index.

    Do this instead

    Select indices 1, 2, and 3 for a slice whose boundaries are 1 and 4.

  • Common mistake

    Trying to replace one character in place.

    What it looks like

    Assuming an individual character can be assigned a new value inside the original string.

    Why it fails

    Strings are immutable, so individual characters cannot be changed in place.

    Do this instead

    Create a new string through concatenation or a string method.

  • Common mistake

    Confusing traversal with search.

    What it looks like

    Continuing through every character after the target has already been found.

    Why it fails

    Search is a specialized traversal that stops when the target is found.

    Do this instead

    Stop at the match when the task only requires finding whether or where the target occurs.

  • Common mistake

    Forgetting the object in dot notation.

    What it looks like

    Thinking only about a method name without identifying the string on which it operates.

    Why it fails

    Dot notation invokes the method on a specific string object.

    Do this instead

    Identify the string or string-holding variable, then the dot, method name, and parentheses.

Practice the Patterns

EASY

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.
MEDIUM

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

  1. 1
    Strings are sequences of characters arranged in order and identified by integer indices beginning at 0.
  2. 2
    An index selects one item, while a slice selects a contiguous range with an inclusive start and exclusive end.
  3. 3
    Strings are immutable, so transformations create new strings instead of changing individual characters in place.
  4. 4
    Dot notation invokes a method on a particular string object.
  5. 5
    Traversal visits every character systematically, while search is a traversal that stops when a target is found.
  6. 6
    The empty string is a valid string with length 0 and no items.

At a glance

  • 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.

Related concepts

More concepts starting with S