Strings as Data Structures
In Part 1 (Module 2: Variables & Data Types), we learned about strings as a text data type — how to create text using quotes, combine sentences with +, check total length using len(), and convert between types.
In Part 2, we explore strings as an ordered collection of characters. Instead of viewing a string as a single chunk of text, Python allows us to look inside it as a sequence of individual letters arranged in a specific order, where every character has its own position.
The Power of String Manipulation
Because strings are structured sequences of characters, Python lets us manipulate them in incredibly powerful ways:
- Grab individual letters (Indexing): Get any specific character using its position number. For example, grabbing
"P"from"Python"usingword[0]. - Cut out sections of text (Slicing): Extract a whole slice of a string. For example, grabbing
"Py"from"Python"usingword[0:2]. - Clean up messy input (Stripping): Instantly remove accidental spaces typed by users at the beginning or end of text.
- Search and Replace: Find out if a word exists inside a long paragraph, or replace specific words automatically.
- Split and Join: Break a sentence into separate words, or glue separate words back together into a single string.
In the next two lessons, we will learn how to use these features step-by-step!
Quick Summary
- String as Sequence: Strings are ordered sequences of characters where each letter occupies a fixed index position.
- Core Manipulations: Python provides indexing (single characters), slicing (sub-strings), searching, splitting, and joining.
- Immutability: Once created in memory, characters inside a string cannot be modified directly in place.
What's Next?
Let's dive into String Indexing & Slicing to learn how to grab specific characters and slices of text in the next lesson!