String Methods: split() and join()
Unpacking is a syntactic feature that assigns multiple variables from a sequence in one statement, equivalent to index-based assignment.
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.
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
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
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.
name1, name2 = ["Ada", "Grace"]
From Strings to Unpackable Parts
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.
first is Ada
last is LovelaceThe important sequence operation occurs after split(): the produced parts are assigned from left to right to the target variables.
Joining Parts into One String
join() combines multiple strings from a sequence into one string. A separator is used as the connection between the sequence elements.
Ada Lovelacesplit() 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
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.
Using two target variables for a sequence with three elements
The sequence contains more elements than the two target variables can receive, so the counts do not match.
Fix:
Provide three target variables or use a sequence containing two elements.Using three target variables for a sequence with two elements
There are fewer sequence elements than target variables.
Fix:
Make the number of targets equal to the number of sequence elements.Assuming split() always produces the number of parts you expected
The resulting sequence contains three parts, but the assignment has only two target variables.
Fix:
Check the parts produced by split() before unpacking them, then adjust the targets or the input.
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
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.
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
- Unpacking assigns multiple variables from a sequence in one statement.
- The assignment follows element order: the first target receives the first element, the second target receives the second element, and so on.
- Unpacking is syntactic sugar for equivalent index-based assignments.
- split() produces a list of substrings that can be unpacked when the element count matches the target count.
- join() combines string elements into one string using a separator.
- A mismatch between target variables and sequence elements causes a ValueError.
Key Takeaways
- 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.