Concepts / Understanding Strings as Sequences

Understanding Strings as Sequences

A list of characters is fundamentally different from a string, even though both are sequences.

  • Programming

One Sequence, Different Structures

A string is a sequence of text characters. A list of characters is also a sequence, but it is a different kind of structure. The difference matters because your goal determines how the text should be represented: use list() when you need individual characters, split() when you need words or larger segments, and join() when you need to build one string from several string elements.

list()StringhelloCharacter list[h, e, l, l, o]
What does the same text contain when represented as one string compared with a list of individual characters?

Both representations preserve sequence order, but they do not represent the sequence in the same structural form.

Mapping Characters with list()

Call list() with a string when the desired result is one list element for each individual character. The characters remain in their original sequence order, so the first character occupies the first list position, the second character occupies the second position, and so on.

python
Output
['h', 'e', 'l', 'l', 'o']
nextnextnextnexthposition 0eposition 1lposition 2lposition 3oposition 4
How does one string map to separate character elements, and what position does each character occupy?

Splitting Words and Segments

Use the split() method when the desired result is a list of words or larger segments. A delimiter identifies where one segment ends and the next begins. The delimiter may be supplied to split() so the string is divided at that chosen boundary.

python
Output
['red', 'green', 'blue']
split at commasplit at commasplit at commared,green,blueredgreenblue
How does a delimiter determine where one segment ends and the next segment begins?

Choosing a Segment Boundary

Turn the text "2026-09-27" into separate segments using the hyphen as the delimiter.

Identify the boundary: The hyphen separates the larger pieces of text.

Apply split(): Use the hyphen as the delimiter passed to split().

Read the result: The result is a list containing the segments on either side of each hyphen.

['2026', '09', '27']

Rebuilding Text with join()

Use join() to combine a list of strings into one string. The separator is the string on which join() is called, and that separator is placed between the list elements as they are combined.

python
Output
red, green, blue
first elementseparator betweenseparator betweeninserted between elementsred,red, green, bluegreenblue
How does a separator get placed between list elements as they are combined into one string?

Selecting the Right Conversion

Choose the operation by asking what each resulting element should represent. If an element should represent one character, use list(). If an element should represent a word or segment separated by a delimiter, use split(). If you already have string elements and want one finished string, use join().

useuseuseIndividual characterslist()Words or segmentssplit()One combined stringjoin()
Which conversion should be used to get characters, separate words or segments, or rebuild text from string pieces?
Processing goalOperationResult structure
Work with individual characterslist(string)A list of character elements
Separate words or segmentsstring.split(delimiter)A list of text segments
Build one string from string elementsseparator.join(list_of_strings)One combined string

Match the operation to the structure you need.

Mistakes Beginners Make

  • Using list() when the goal is to separate words or segments

    list() produces individual characters, including the commas, rather than larger segments.

    Fix: Use "red,green,blue".split(",") when commas define the segment boundaries.

  • Using split() when the goal is to obtain individual characters

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

    Fix: Use list("hello") for a list of individual characters.

  • Calling join() on the list

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

    Fix: Write ",".join(["red", "green"]).

  • Forgetting that the separator controls the rebuilt text

    An empty separator places nothing between the elements.

    Fix: Choose the separator that matches the desired output, such as ", " or " ".

Practice the Choice

EASY

For each goal, choose list(), split(), or join(): obtain each character from "code"; separate "one|two|three" at the vertical bar; combine ["one", "two", "three"] with a single space between elements.

Hints
  • Ask whether the desired elements are characters, larger segments, or an already existing list of strings.
  • For join(), remember that the separator appears before .join().

Checking the Three Goals

Select the operation for each of the three text-processing tasks.

Characters: The target elements are individual characters, so use list("code").

Segments: The vertical bar marks the boundaries between segments, so use "one|two|three".split("|").

Rebuilt text: The input is already a list of strings, and a space must be placed between elements, so use " ".join(["one", "two", "three"]).

list("code") produces ['c', 'o', 'd', 'e']; splitting produces ['one', 'two', 'three']; joining with a space produces "one two three".

Key Takeaways

  1. A string and a list of characters are both sequences, but they are different structures.
  2. Use list() to convert a string into individual character elements.
  3. Use split() to divide a string into words or segments at a delimiter.
  4. Use join() to combine a list of strings, with the separator placed between elements.
  5. Remember that join() is called on the separator string, not on the list.

Key Takeaways

  • Use list() for character-level conversion.
  • Use split() when delimiters should divide text into words or segments.
  • Use join() to rebuild one string from a list of strings.
  • Choose the operation according to the structure your next processing step requires.
  • Call join() on the separator string.