Concepts / Mutability: Strings vs Lists

Mutability: Strings vs Lists

A list of characters is fundamentally different from a string, even though both are sequences.

  • Programming

One Sequence, Two Behaviors

A string and a list of characters are both sequences, but they are not the same kind of sequence. The difference matters when your goal is to inspect individual characters, divide text into meaningful segments, or rebuild text after working with separate pieces.

containscontainsStringhelloText sequenceone stringCharacter list['h', 'e', 'l', 'l', 'o']Separate elementsone character per position
What changes when the same text is represented as one string or as separate list elements?

Tracing Character Conversion

Use list() when the desired result is one list element for every character in the original string. The characters keep their order, so the first character becomes the first list element, the second character becomes the second element, and so on.

python
Output
['c', 'o', 'd', 'e']
list()list()list()list()codeoriginal string0c1o2d3e
How does one string become a list containing one character at each position?

What do you think happens?

What will list("A B") contain?

  • ['A', 'B']
  • ['A', ' ', 'B']
  • ['A B']
Reveal answer

Answer: ['A', ' ', 'B']

list() breaks the string into individual characters. The space is a character in the original string, so it becomes its own list element.

Choosing the Boundary

Use split() when the important units are words or larger segments rather than individual characters. The delimiter determines where the original string is divided. An optional delimiter lets you specify the boundary that separates the resulting pieces.

python
Output
['red', 'green', 'blue']
split(",")split(",")split(",")red,green,blueredgreenblue
How does the delimiter determine where the original string is divided into separate list elements?
GoalOperationResult structure
Get individual characterslist(text)A list with one character per element
Get words or delimiter-based segmentstext.split(delimiter)A list divided at the delimiter
Combine string elementsseparator.join(items)One resulting string

Rebuilding Text with join

Use join() when you have a list of strings and need one combined string. The separator is the string that calls join, and it is placed between the list elements. This is the opposite direction from splitting text into separate pieces.

Combining words with spaces

Combine the list ['learn', 'by', 'doing'] into one string with spaces between the words.

Choose the separator: The desired separator is one space, written as " ".

Call join on the separator: Write the separator first and call join with the list of strings: " ".join(words).

Place the elements in order: join places the separator between learn, by, and doing.

learn by doing

python
Output
learn by doing
joinplaces between itemslearn | by | doinglist elementsspaceseparator stringlearn by doingone string
How does a separator connect the items in a list to produce one resulting string?

Common Conversion Mistakes

  • Using list() when the goal is to get words or delimiter-based segments.

    list() works character by character, so the comma and each individual letter become separate elements.

    Fix: Use "red,green".split(",") when the comma marks the desired boundary.

  • Using split() when the goal is one character per list element.

    split() is intended to divide text into words or segments at a delimiter, not to express the character-by-character conversion.

    Fix: Use list("code") for a list of individual characters.

  • Calling join() on the list.

    join is called on the separator string, not on the list.

    Fix: Write ",".join(["red", "green"]).

  • Forgetting that the separator is inserted between elements.

    The separator controls how the existing list elements are connected in the resulting string.

    Fix: Choose the separator that matches the required output, such as a space, comma, or hyphen.

Selecting the Right Representation

Start with the data-processing goal rather than the function name. If every character must become a separate element, use list(). If a delimiter marks meaningful boundaries such as words or segments, use split(). If separate string elements need to become one piece of text, use join() with the desired separator.

EASY

Choose the appropriate operation for each task: turn "ABC" into separate characters; turn "east|west|north" into segments; combine ["fast", "focused", "clear"] with hyphens.

Hints
  • The first task needs one element per character.
  • The second task uses the vertical bar as its delimiter.
  • For the third task, place the hyphen string before join.

Matching operations to goals

Select list(), split(), or join() for three different transformations.

Individual characters: Use list("ABC") because the target is one list element for each character.

Delimiter-based segments: Use "east|west|north".split("|") because the vertical bar identifies the boundaries.

One combined string: Use "-".join(["fast", "focused", "clear"]) because the list elements must be connected with hyphens.

list() handles character conversion, split() handles delimiter-based division, and join() handles recombination.

Key Takeaways

  1. A list of characters is fundamentally different from a string, even though both are sequences.
  2. Use list() to convert a string into individual character elements.
  3. Use split() to divide a string into words or segments at a delimiter.
  4. Use join() to combine a list of strings, remembering that the separator calls join.
  5. Choose the operation according to whether you need characters, delimiter-based segments, or one combined string.

Key Takeaways

  • Strings and lists of characters are both sequences, but they represent data differently.
  • list() produces one list element for each character in a string.
  • split() uses a delimiter to produce words or larger segments.
  • join() uses a separator string to combine list elements into one string.
  • Choose the operation based on the structure needed for the next processing step.