Module 7: Strings in Depth -- Quick Reference
A printable reference card covering string methods, slicing, searching, and common patterns.
Case Conversion
| Method | Description | Example |
|---|---|---|
.upper() | All uppercase | "hello".upper() → "HELLO" |
.lower() | All lowercase | "HELLO".lower() → "hello" |
.title() | Capitalize each word | "hello world".title() → "Hello World" |
.capitalize() | Capitalize first letter | "hello world".capitalize() → "Hello world" |
.swapcase() | Swap upper/lower | "Hello".swapcase() → "hELLO" |
Whitespace and Stripping
| Method | Description | Example |
|---|---|---|
.strip() | Remove both ends | " hi ".strip() → "hi" |
.lstrip() | Remove left side | " hi ".lstrip() → "hi " |
.rstrip() | Remove right side | " hi ".rstrip() → " hi" |
.strip(chars) | Strip specific chars | "###hi###".strip("#") → "hi" |
Searching
| Method | Description | Not Found |
|---|---|---|
.find(sub) | Index of first occurrence | Returns -1 |
.rfind(sub) | Index of last occurrence | Returns -1 |
.index(sub) | Index of first occurrence | Raises ValueError |
.rindex(sub) | Index of last occurrence | Raises ValueError |
sub in s | Check existence | Returns False |
.count(sub) | Count occurrences | Returns 0 |
Replacing and Transforming
| Method | Description | Example |
|---|---|---|
.replace(old, new) | Replace all occurrences | "aab".replace("a","x") → "xxb" |
.replace(old, new, n) | Replace first n | "aab".replace("a","x",1) → "xab" |
Checking Methods
| Method | Returns True When |
|---|---|
.startswith(sub) | String starts with sub |
.endswith(sub) | String ends with sub |
.isdigit() | All characters are digits |
.isalpha() | All characters are letters |
.isalnum() | All characters are letters or digits |
.islower() | All cased characters are lowercase |
.isupper() | All cased characters are uppercase |
.isspace() | All characters are whitespace |
String Slicing
| Syntax | Description | Example (s = "Python") |
|---|---|---|
s[a:b] | From index a to b-1 | s[0:3] → "Pyt" |
s[:b] | From start to b-1 | s[:2] → "Py" |
s[a:] | From a to end | s[4:] → "on" |
s[-n:] | Last n characters | s[-3:] → "hon" |
s[::2] | Every other character | s[::2] → "Pto" |
s[::-1] | Reversed | s[::-1] → "nohtyP" |
Splitting and Joining
| Method | Description | Example |
|---|---|---|
.split() | Split on whitespace | "a b c".split() → ["a","b","c"] |
.split(sep) | Split on separator | "a,b,c".split(",") → ["a","b","c"] |
.split(sep, n) | Split max n times | "a,b,c".split(",",1) → ["a","b,c"] |
.splitlines() | Split on line breaks | "a\nb".splitlines() → ["a","b"] |
sep.join(list) | Join list with separator | "-".join(["a","b"]) → "a-b" |
Special Strings
| Type | Syntax | Description |
|---|---|---|
| f-string | f"Hello {name}" | Formatted string with embedded expressions |
| Raw string | r"C:\path" | Backslashes treated literally |
| Multiline | """text""" | String spanning multiple lines |
Escape Characters
| Escape | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\\ | Literal backslash |
\" | Literal double quote |
\' | Literal single quote |