String Manipulation
In Python, a String (str) is a sequence of Unicode characters. Whether you are building a web server, analyzing data, or writing a simple script, text processing will be your most frequent task.
In Python, strings are not just “arrays of characters”—they are sophisticated objects with a rich set of behaviors.
1. Defining Strings
Section titled “1. Defining Strings”Python offers multiple ways to define strings, each serving a different purpose.
- Single/Double Quotes:
'Hi'or"Hi". Use double quotes if your text contains a single quote:"It's Python". - Triple Quotes:
"""...""". Used for multiline strings. These preserve all whitespace and newlines exactly as typed.
2. Immutability: The Constant Nature
Section titled “2. Immutability: The Constant Nature”Strings in Python are Immutable. This means that once a string object is created in memory, its contents can never be changed.
name = "Python"# name[0] = "C" # Raises TypeError: 'str' object does not support item assignmentThe Performance Cost of Concatenation
Section titled “The Performance Cost of Concatenation”Because strings are immutable, every time you do s1 + s2, Python must:
- Allocate a brand new block of memory large enough for both.
- Copy the characters from
s1. - Copy the characters from
s2.
Best Practice: If you are joining many strings (e.g., inside a loop), do not use +. Instead, collect them in a list and use "".join(list_of_strings). This is significantly faster ($O(n)$ vs $O(n^2)$).
3. Indexing & Slicing
Section titled “3. Indexing & Slicing”Strings are sequences, meaning every character has a position (index).
| P | y | t | h | o | n |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 |
| -6 | -5 | -4 | -3 | -2 | -1 |
Slicing Syntax: [start:stop:step]
Section titled “Slicing Syntax: [start:stop:step]”start: Inclusive.stop: Exclusive (up to but not including).step: The increment (negative reverses the string).
msg = "Technogic"
print(msg[0:4]) # "Tech"print(msg[6:]) # "gic"print(msg[::-1]) # "cigonhceT" (Reverse)4. Modern Formatting: f-strings
Section titled “4. Modern Formatting: f-strings”Introduced in Python 3.6, f-strings (formatted string literals) are the fastest and most readable way to interpolate variables.
user = "Alice"score = 98.5print(f"User {user.upper()} scored {score:.0f}%")# Output: User ALICE scored 99%Under the Hood: f-strings are evaluated at runtime. This means you can put any valid Python expression inside the curly braces {}.
5. Under the Hood: String Interning
Section titled “5. Under the Hood: String Interning”To save memory, Python performs String Interning. If you create two identical short strings that look like identifiers (no spaces/symbols), Python may point both variables to the same memory address.
a = "hello"b = "hello"print(a is b) # True (Shared memory!)
c = "hello world!"d = "hello world!"print(c is d) # False (Too complex to intern automatically)6. Essential Methods
Section titled “6. Essential Methods”| Method | Use Case |
|---|---|
.strip() | Removes whitespace from both ends. |
.split() | Breaks a string into a list based on a delimiter. |
.replace() | Swaps all occurrences of one substring for another. |
.find() | Returns the index of a substring (or -1 if not found). |
.encode() | Converts the string to bytes (usually UTF-8). |
line = " ID, NAME, ROLE "# Chaining methodsparts = line.strip().lower().split(", ")print(parts) # ['id', 'name', 'role']