Converting Lists Back to Strings
A list of characters is fundamentally different from a string, even though both are sequences.
Two Similar Sequences
A string and a list of characters can contain the same visible letters, but they are not the same kind of value. A string is one sequence of characters. A character list is a list whose separate elements are one-character strings. Converting between them is useful when you need to work with individual elements and later rebuild one string.
Creating Character Elements
The list() function converts a string into a list in which each element is a single-character string. For the string hello, the resulting list has five elements: h, e, l, l, and o. The elements keep their original order, and each element can be accessed by an index beginning at 0.
characters contains the five single-character strings h, e, l, l, and o in that order.Naming the Converted Value
| Name | Guidance | Reason |
|---|---|---|
| characters | Recommended | Describes the contents clearly |
| char_list | Recommended | Shows that the value is a list of characters |
| list | Avoid | Shadows the built-in list function |
| l | Avoid | Can be visually confused with the number 1 |
Variable names should communicate the role of the value without hiding or confusing built-in names.
Rebuilding One String
join is a string method that combines the elements of a list or another sequence into one string using a specified delimiter. Its syntax is delimiter.join(list). The method is invoked on the delimiter, not on the list.
The delimiter is the string placed between adjacent elements. With a space as the delimiter, separate elements receive spaces between them. The delimiter is not placed before the first element or after the last element.
result becomes the single string I J K TEC. The spaces appear only between neighboring list elements.Tracing the Join Flow
Conceptually, join takes the elements in their existing order and places the chosen delimiter between each neighboring pair. It then produces one continuous string. This is why join reverses the split operation described in the concept: split separates a string into pieces, while join combines sequence elements into one string.
Controlling the Separator
An empty string is a useful delimiter when elements should be concatenated with nothing between them. For a character list, this rebuilds the original word without inserting spaces or punctuation. Other delimiters let you build readable text from separate pieces, such as a comma followed by a space or a dash.
word becomes hello, spaced becomes h e l l o, and dashed becomes h-e-l-l-o.Mistakes to Avoid
Common mistake Treating a character list as if it were the original string
What it looks likeA value containing separate one-character strings is still a list, even though its visible letters resemble a word.
Why it failsThe container and the elements have a different structure. A character list has separate indexed elements.
Do this insteadUse "".join(characters) when one string is required.
Common mistake Calling join on the list
What it looks likecharacters.join(" ")Why it failsjoin is a string method invoked on the delimiter.
Do this insteadWrite the delimiter first: " ".join(characters).Common mistake Expecting the delimiter at the beginning or end
What it looks likeUsing a space delimiter does not create a leading or trailing space.
Why it failsThe delimiter appears only between adjacent elements.
Do this insteadPredict separators only in the gaps between elements.
Common mistake Using list as a variable name
What it looks likelist = list("hello")Why it failsThe variable name shadows the built-in list function.
Do this insteadUse characters or char_list instead.Common mistake Using an ambiguous one-letter name
What it looks likel = list("hello")Why it failsThe letter l can be visually confused with the number 1.
Do this insteadUse a descriptive name such as characters.
Practical Construction
Build a Label from Separate Parts
Combine the list ["chapter", "3", "strings"] into one string with a colon and a space between elements.
- 1Choose the delimiter
The required separator is a colon followed by a space.
- 2Place the delimiter before join
The join syntax is
delimiter.join(sequence), so the separator receives the method call. - 3Combine the elements
The list elements remain in order, and the delimiter appears between each adjacent pair.
The resulting string is chapter: 3: strings.
Predict the result of "".join(["c", "o", "d", "e"]). Then predict the result of ", ".join(["red", "green", "blue"]). For each result, identify where the delimiter appears.
Hints
- The empty string contributes no visible characters between elements.
- The comma-and-space delimiter appears only between neighboring elements.
Key Takeaways
- 1
list(string)creates a list whose elements are single-character strings. - 2Character-list elements keep their order and are accessed with indexes beginning at 0.
- 3A character list is structurally different from one string, even when both contain the same characters.
- 4join is called on the delimiter:
delimiter.join(sequence). - 5The delimiter appears only between elements; an empty string joins elements with no separator.
- 6Clear names such as characters and
char_listavoid built-in conflicts and visual ambiguity.
At a glance
- A string and a list of characters are different sequence structures.
- The
list()function creates one-character string elements in their original order. - Use descriptive variable names instead of list or visually ambiguous names such as l.
- Use
delimiter.join(sequence)to combine elements into one string. - Changing the delimiter changes the separators between elements, while an empty delimiter concatenates them directly.