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
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.
Separate the characters: The string's characters occur in this order: h, e, l, l, o.
Create one list element per character: The converted list contains one single-character string for each character in the original string.
Start 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 |
Common Prediction Errors
Treating the converted result as one list element containing the whole string.
list() creates a list where each element is a single-character string.
Fix:
Represent the result as one list element for h, one for e, one for each l, and one for o.Starting the first character at index 1.
Indexes for the resulting list start at 0.
Fix:
Place the first character at index 0 and continue in the original character order.Using list as a variable name.
The name shadows the built-in function list and can create confusion when list() is needed.
Fix:
Use a descriptive name such as characters.Using the visually ambiguous name l.
The name can be difficult to distinguish visually from other characters.
Fix:
Choose 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
- A string and a list of characters are different structures even though both are sequences.
- list() converts a string into a list containing one single-character string per character.
- The resulting list preserves character order, and indexing begins at 0.
- Use descriptive variable names, avoid shadowing built-in functions such as list, and avoid visually ambiguous names such as l.
Key Takeaways
- 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.