Concepts / String Methods: split() and join()

String Methods: split() and join()

Unpacking lets one assignment statement distribute the elements of a sequence across multiple variables.

  • Programming

One Assignment, Several Values

Unpacking lets one assignment statement distribute the elements of a sequence across multiple variables. Instead of assigning each element separately by index, you place the target variables on the left side and the sequence on the right side.

python

The first element is assigned to first, and the second element is assigned to second. The same unpacking idea applies to tuples, lists, strings, and other sequence types.

Mapping Elements to Names

sequence[10, 20]10element 0firstreceives 1020element 1secondreceives 20index 0index 1assignassign
How do the individual elements of a sequence map to the variables on the left side of an assignment?

Read an unpacking assignment from left to right. The first target receives the first sequence element, the second target receives the second element, and so on. The variables do not receive the whole sequence; they receive the individual elements mapped to their positions.

Unpacking and Index Assignment

name1, name2 =sequencename1 = sequence[0]name2 = sequence[1]equivalent toequivalent to
How is multiple-variable unpacking equivalent to assigning individual sequence indexes?

Unpacking is syntactic sugar for index-based assignment. In other words, the shorter unpacking form represents the same element-by-element mapping that could be written with indexes.

Code walkthrough
name1, name2 = ["Ada", "Grace"]
  1. 1
    The sequence has two elements.
  2. 2
    name1 receives the element at index 0.
  3. 3
    name2 receives the element at index 1.
  4. 4
    The unpacking form expresses the two index-based assignments in one statement.

From Strings to Unpackable Parts

one stringAda Lovelacesplit()list of substrings[Ada, Lovelace]firstAdalastLovelacedividereturnsunpackunpack
How does split() divide one string into a list of substrings that can then be assigned to multiple variables?

split() transforms one string into a list of substrings. That resulting sequence can be unpacked when the number of target variables matches the number of parts.

python
Output
first is Ada
last is Lovelace

The important sequence operation occurs after split(): the produced parts are assigned from left to right to the target variables.

Joining Parts into One String

string sequence[Ada, Lovelace]separatorspacejoin()combined stringAda Lovelaceelementsbetween elementscombine
How do multiple string elements move through a separator to become one combined string?

join() combines multiple strings from a sequence into one string. A separator is used as the connection between the sequence elements.

python
Output
Ada Lovelace

split() and join() move in opposite directions: split() turns one string into multiple parts, while join() turns multiple string elements into one string. When split() creates exactly the parts expected by an unpacking assignment, the two ideas can be used together in a clear transformation.

When Counts Do Not Match

two targetsfirst, secondValueErrorelement count mismatchthree elements[a, b, c]unpacktoo many elements
What happens when a sequence contains more or fewer elements than there are target variables?

A correct unpacking assignment requires an exact count match. If the sequence has more or fewer elements than the target variables, the mismatch causes a ValueError.

  • Common mistake

    Using two target variables for a sequence with three elements

    What it looks like
    first, second = ["a", "b", "c"]
    Why it fails

    The sequence contains more elements than the two target variables can receive, so the counts do not match.

    Do this instead

    Provide three target variables or use a sequence containing two elements.

  • Common mistake

    Using three target variables for a sequence with two elements

    What it looks like
    first, middle, last = ["a", "b"]
    Why it fails

    There are fewer sequence elements than target variables.

    Do this instead

    Make the number of targets equal to the number of sequence elements.

  • Common mistake

    Assuming split() always produces the number of parts you expected

    What it looks like
    first, last = "Ada Byron Lovelace".split()
    Why it fails

    The resulting sequence contains three parts, but the assignment has only two target variables.

    Do this instead

    Check the parts produced by split() before unpacking them, then adjust the targets or the input.

Best practice

When an unpacking assignment fails, count both sides before changing the syntax: count the target variables on the left and the elements in the sequence on the right. With split(), inspect how many substrings the input produces.

Practice the Transformation

EASY

Consider the sequence ["red", "green"]. Write one unpacking assignment that stores its elements in the variables first_color and second_color. Then describe the equivalent index-based assignments.

Hints
  • Use one target variable for each element.
  • The first target corresponds to index 0.
  • The second target corresponds to index 1.
MEDIUM

A string is split into three parts: ["2026", "09", "27"]. Write an unpacking assignment for the variables year, month, and day. What error would result if you used only year and month?

Hints
  • The number of target variables must equal the number of parts.
  • Count the three elements before writing the assignment.

Key Takeaways

  1. 1
    Unpacking assigns multiple variables from a sequence in one statement.
  2. 2
    The assignment follows element order: the first target receives the first element, the second target receives the second element, and so on.
  3. 3
    Unpacking is syntactic sugar for equivalent index-based assignments.
  4. 4
    split() produces a list of substrings that can be unpacked when the element count matches the target count.
  5. 5
    join() combines string elements into one string using a separator.
  6. 6
    A mismatch between target variables and sequence elements causes a ValueError.

At a glance

  • Unpacking maps sequence elements to multiple variables in one statement.
  • Tuples, lists, strings, and other sequence types can be unpacked using the same element-to-variable pattern.
  • split() creates parts that can be unpacked, while join() combines string elements into one string.
  • The number of target variables must exactly match the number of sequence elements.
  • Unpacking expresses the same element mapping as separate index-based assignments.

Related concepts

More concepts starting with S