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.

StringhelloText sequenceone stringCharacter list['h', 'e', 'l', 'l', 'o']Separate elementsone character per positioncontainscontains
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']
codeoriginal string0c1o2d3elist()list()list()list()
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']
red,green,blueredgreenbluesplit(",")split(",")split(",")
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

The three operations produce different representations for different processing goals.

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.

Worked example

Combining words with spaces

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

  1. 1
    Choose the separator

    The desired separator is one space, written as " ".

  2. 2
    Call join on the separator

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

  3. 3
    Place the elements in order

    join places the separator between learn, by, and doing.

Result

learn by doing

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

Common Conversion Mistakes

  • Common mistake

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

    What it looks like
    list("red,green")
    Why it fails

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

    Do this instead

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

  • Common mistake

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

    What it looks like
    "code".split()
    Why it fails

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

    Do this instead

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

  • Common mistake

    Calling join() on the list.

    What it looks like
    ["red", "green"].join(",")
    Why it fails

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

    Do this instead
    Write ",".join(["red", "green"]).
  • Common mistake

    Forgetting that the separator is inserted between elements.

    What it looks like
    "-".join(["2026", "09", "27"])
    Why it fails

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

    Do this instead

    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.
Worked example

Matching operations to goals

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

  1. 1
    Individual characters

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

  2. 2
    Delimiter-based segments

    Use "east|west|north".split("|") because the vertical bar identifies the boundaries.

  3. 3
    One combined string

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

Result

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

Key Takeaways

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

At a glance

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

Related concepts

More concepts starting with M