Concepts / Understanding Strings as Sequences

Understanding Strings as Sequences

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

  • 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.

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

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']
hposition 0eposition 1lposition 2lposition 3oposition 4nextnextnextnext
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']
red,green,blueredgreenbluesplit at commasplit at commasplit at comma
How does a delimiter determine where one segment ends and the next segment begins?
Worked example

Choosing a Segment Boundary

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

  1. 1
    Identify the boundary

    The hyphen separates the larger pieces of text.

  2. 2
    Apply split()

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

  3. 3
    Read the result

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

Result

['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
red,red, green, bluegreenbluefirst elementseparator betweenseparator betweeninserted between elements
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().

Individual characterslist()Words or segmentssplit()One combined stringjoin()useuseuse
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

  • Common mistake

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

    What it looks like
    list("red,green,blue")
    Why it fails

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

    Do this instead

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

  • Common mistake

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

    What it looks like
    "hello".split()
    Why it fails

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

    Do this instead

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

  • Common mistake

    Calling join() on the list

    What it looks like
    ["red", "green"].join(",")
    Why it fails

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

    Do this instead
    Write ",".join(["red", "green"]).
  • Common mistake

    Forgetting that the separator controls the rebuilt text

    What it looks like
    "".join(["red", "green"])
    Why it fails

    An empty separator places nothing between the elements.

    Do this instead

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

Before
["red", "green"].join(",")

The list is treated as if it owns join().

After
",".join(["red", "green"])

The separator string calls join() and receives the list.

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().
Worked example

Checking the Three Goals

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

  1. 1
    Characters

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

  2. 2
    Segments

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

  3. 3
    Rebuilt text

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

Result

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

Key Takeaways

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

At a glance

  • 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.

Related concepts

More concepts starting with U