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 |
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
Treating a character list as if it were the original string
The container and the elements have a different structure. A character list has separate indexed elements.
Fix:
Use "".join(characters) when one string is required.Calling join on the list
join is a string method invoked on the delimiter.
Fix:
Write the delimiter first: " ".join(characters).Expecting the delimiter at the beginning or end
The delimiter appears only between adjacent elements.
Fix:
Predict separators only in the gaps between elements.Using list as a variable name
The variable name shadows the built-in list function.
Fix:
Use characters or char_list instead.Using an ambiguous one-letter name
The letter l can be visually confused with the number 1.
Fix:
Use 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.
Choose the delimiter: The required separator is a colon followed by a space.
Place the delimiter before join: The join syntax is delimiter.join(sequence), so the separator receives the method call.
Combine 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
- list(string) creates a list whose elements are single-character strings.
- Character-list elements keep their order and are accessed with indexes beginning at 0.
- A character list is structurally different from one string, even when both contain the same characters.
- join is called on the delimiter: delimiter.join(sequence).
- The delimiter appears only between elements; an empty string joins elements with no separator.
- Clear names such as characters and char_list avoid built-in conflicts and visual ambiguity.
Key Takeaways
- 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.