Strings as Data Structures
We already learned the basic concepts of strings in Part 1 (Module 2: Variables & Data Types). We know how to create them using quotes, calculate their length with len(), combine them with +, and convert other types into strings.
Now in Part 2, we will look at how to work with strings at an advanced level. In Python, a string is not just a single value; it is treated as a Sequence / Collection of characters (a data structure).
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!