Understanding Strings
A list of characters is fundamentally different from a string, even though both are sequences.
One Sequence, Two Structures
A string and a list of characters can contain the same sequence of characters, but they are not the same kind of thing. A string is one sequence, while a list of characters is a list whose individual elements are single-character strings. That difference matters when you inspect the structure of the data or access its elements by index.
Tracing list() Conversion
What do you think happens?
Suppose the string is hello. What does list() produce when it converts that string?
Reveal answer
A list with one element for each character
The list() function converts a string into a list where each element is a single-character string.
The conversion does not place the entire original string into one list element. Instead, list() creates a list whose elements are the individual characters from the string. For the generated example hello, the resulting character list has five elements: h, e, l, l, and o. Each of those elements is a single-character string.
Reading Character Positions
After conversion, each character in the resulting list is accessible by an index. Indexing starts at 0, so the first character is at index 0, the second character is at index 1, and so on. The list follows the order of the characters in the original string.
Mapping Characters to Indexes
Predict the character list produced from the string hello and identify the character at each index.
- 1Separate the characters
The string's characters occur in this order: h, e, l, l, o.
- 2Create one list element per character
The converted list contains one single-character string for each character in the original string.
- 3Start indexing at zero
The first character, h, is at index 0. The remaining characters follow at indexes 1 through 4.
Index 0 contains h, index 1 contains e, index 2 contains l, index 3 contains l, and index 4 contains o.
To predict a converted character list, preserve the original character order, place each character in its own list element, and label the first element with index 0.
Names That Stay Clear
Variable names should not shadow built-in functions such as list. A name that conflicts with list can make it unclear whether list refers to the built-in conversion function or to a variable. Also avoid visually ambiguous names such as the lowercase letter l, which can be difficult to distinguish from other characters. A descriptive name such as characters makes the purpose of a character list easier to understand.
| Name choice | Why it matters | Clearer approach |
|---|---|---|
| list | Can shadow the built-in function named list | Use a descriptive name such as characters |
| l | Can be visually ambiguous | Use a readable descriptive name |
| characters | Communicates that the value contains characters | Good choice for a character list |
Variable names should preserve access to built-in functions and make the value's purpose easy to recognize.
Common Prediction Errors
Common mistake Treating the converted result as one list element containing the whole string.
What it looks likeExpecting the conversion of hello to contain only one element.
Why it failslist()creates a list where each element is a single-character string.Do this insteadRepresent the result as one list element for h, one for e, one for each l, and one for o.
Common mistake Starting the first character at index 1.
What it looks likeSaying that h is at index 1 in the converted list.
Why it failsIndexes for the resulting list start at 0.
Do this insteadPlace the first character at index 0 and continue in the original character order.
Common mistake Using list as a variable name.
What it looks likeNaming a character list list.
Why it failsThe name shadows the built-in function list and can create confusion when
list()is needed.Do this insteadUse a descriptive name such as characters.
Common mistake Using the visually ambiguous name l.
What it looks likeNaming a character list with the lowercase letter l.
Why it failsThe name can be difficult to distinguish visually from other characters.
Do this insteadChoose a readable descriptive name.
Practice the Conversion
A string contains the characters coding. Predict the character list produced by list(). Then write the character at each index, beginning with index 0. Finally, choose a clear variable name for the resulting list.
Hints
- Keep the characters in their original order.
- Put each character in its own list element.
- The first element uses index 0.
- Avoid list and the visually ambiguous name l.
A reliable prediction process is: identify every character, place the characters into separate list elements in order, assign indexes beginning at 0, and select a descriptive variable name.
Key Takeaways
- 1A string and a list of characters are different structures even though both are sequences.
- 2
list()converts a string into a list containing one single-character string per character. - 3The resulting list preserves character order, and indexing begins at 0.
- 4Use descriptive variable names, avoid shadowing built-in functions such as list, and avoid visually ambiguous names such as l.
At a glance
- A list of characters is fundamentally different from a string, even though both are sequences.
- The
list()function creates one list element for each character in a string. - Character-list indexes begin at 0 and follow the original character order.
- Clear variable names prevent confusion with built-in functions and visually ambiguous names.