String Manipulation Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- String Definition: An immutable sequence of Unicode characters enclosed within single, double, or triple quotes.
- Two-Way Indexing: Positive / Forward Indexing: 0 to len-1 (from left to right); Negative / Backward Indexing: -1 to -len (from right to left).
- String Immutability: Elements of a string cannot be modified or replaced in-place. s[0] = 'X' raises a TypeError.
- String Slicing: str[start : stop : step] extracts substring from start up to stop - 1 stepping by step. Omitting start defaults to 0; omitting stop defaults to length. Reversing a string: s[::-1].
- String Operators: Concatenation (+); Repetition (*); Membership (in, not in); Comparison (==, <, >, etc. based on lexicographical ASCII/Unicode values).
- Built-in String Methods: .len(), .lower(), .upper(), .title(), .capitalize(), .count(sub), .find(sub) (returns index or -1), .index(sub) (returns index or raises ValueError), .endswith(s), .startswith(s), .strip(), .lstrip(), .rstrip(), .replace(old, new), .split(sep), sep.join(list).
- Character Testing Methods: .isalnum() (alphanumeric), .isalpha() (alphabetic only), .isdigit() (digits only), .islower(), .isupper(), .isspace().
Chapter Formulas & Equations (10 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | String Slicing Indexing Parameter Formula |
|
Universal sequence slicing mechanism in Python; handles both positive and negative indexing gracefully. |
| 2 | String Reversal Idiom Formula |
|
Standard idiomatic Python construct for reversing strings or checking for palindromes (s == s[::-1]). |
| 3 | Two-Way Dual Indexing Relationship |
|
For any string of length L, the first character has index 0 and -L, while the last character has index L-1 and -1. |
| 4 | String Length & Index Boundaries |
|
Direct index access past boundaries triggers an IndexError, though slicing beyond boundaries handles truncation without error. |
| 5 | Lexicographical Comparison Order |
|
String comparisons compare character by character using ordinal Unicode values from left to right. |
| 6 | Find vs Index Method Difference |
|
Critical functional distinction: .find() is safer for searching, while .index() requires try-except error handling. |
| 7 | Split & Join Invertible Transformations |
|
The .split() method breaks text into a list of words, and .join() recombines a list of strings into text. |
| 8 | String Strip Whitespace Removal |
|
Crucial method for cleaning user input and parsing raw files before processing. |
| 9 | Sub-string Count Non-Overlapping Rule |
|
For example, 'banana'.count('an') returns 2, not 3, because counting does not overlap matched characters. |
| 10 | Palindrome Verification Equation |
|
Canonical interview and board exam pattern to test for symmetric word structure. |
CBSE Board Exam Tips & Frequent Pitfalls
- Remember that strings are immutable. You cannot do s[0] = 'a'; doing so will raise a TypeError.
- Be careful with .find() vs .index(): .find() returns -1 if the substring is absent, while .index() raises a ValueError.
- Remember that slicing never throws an IndexError even if indices are outside bounds: e.g. 'hello'[1:100] safely returns 'ello'.
Step-by-Step Worked Board Exam Examples
Example 1: Given s = 'Computer', evaluate: 1. s[1:5], 2. s[-4:], 3. s[::-2], 4. s[::3].
s = 'Computer': Indices: C(0), o(1), m(2), p(3), u(4), t(5), e(6), r(7).
1. s[1:5]: Characters from index 1 to 4 -> 'ompu'.
2. s[-4:]: Negative index -4 is 'u' (-4, -3, -2, -1) -> 'uter'.
3. s[::-2]: Reversed from end with step 2: indices 7('r'), 5('t'), 3('p'), 1('o') -> 'rtpo'.
4. s[::3]: From start with step 3: indices 0('C'), 3('p'), 6('e') -> 'Cpe'.
Example 2: What will be the output of the following code? text = 'CBSE Board 2026' print(text.isalnum()) print(text.replace(' ', '-').isalnum())
text contains spaces (' '). .isalnum() checks if ALL characters are either letters or numbers. Spaces are neither, so text.isalnum() returns False.
text.replace(' ', '-') produces 'CBSE-Board-2026'. The hyphen (-) is a punctuation symbol, not alphanumeric, so .isalnum() still returns False.
Common Mistakes to Avoid in Exam
- Attempting to modify a character directly via s[0] = 'h'. Strings are immutable in Python; assign a new string instead: s = 'h' + s[1:].
- Forgetting that .split() defaults to splitting by any whitespace (spaces, tabs, newlines) and removes consecutive whitespace.
- Assuming 'apple' > 'Apple' is False. It is TRUE because lowercase 'a' (ASCII 97) is greater than uppercase 'A' (ASCII 65).
Target Practice Questions
Q1. What is the output of 'Hello World'.split()?
Correct Answer: ['Hello', 'World'].
Q2. What does '*'.join(['a', 'b', 'c']) evaluate to?
Correct Answer: 'a*b*c'.
Q3. What will 'Python'[-1] return?
Correct Answer: 'n' (the last character).
Frequently Asked Questions
Q. Why are strings immutable in Python?
For performance optimization, memory efficiency (string interning), thread safety, and allowing strings to be used as dictionary keys and set elements.
Q. What does s.title() do?
Capitalizes the first character of each word in the string and converts the remaining characters to lowercase.
Q. What is the difference between isalpha() and isalnum()?
isalpha() returns True only if all characters are letters; isalnum() returns True if characters are letters or digits.
Q. How do you check if a string starts with a specific prefix?
Using the .startswith(prefix) method, which returns True or False.