Core Module
12 min forge
Python String Formatting and Methods
Beyond concatenation. Learn f-strings, interpolation, and essential high-performance string manipulation.
Python String Formatting and Methods
π‘οΈ What are Strings in Python?
Strings in Python are immutable sequences of Unicode characters. Because they are immutable, every "modification" actually creates a new string object in memory.
β° When to Use
- Dynamic Content: Injecting variables into templates using f-strings (introduced in Python 3.6).
- Data Sanitization: Using methods like
.strip(),.replace(), and.lower()to normalize input. - Parsing: Using
.split()and.join()for text processing.
π Complexity & Performance
- Concatenation: Using
+in a loop is $O(N^2)$ because each step creates a new string. Use''.join(list_of_strings)for $O(N)$ efficiency. - f-strings: These are the fastest formatting method because they are evaluated at runtime as part of the bytecode rather than a function call like
.format(). - String Interning: Python automatically caches (interns) short, compile-time strings to save memory and speed up comparisons.
π» Code Example: Modern Formatting
python Standardname = "Forge" level = 99 # 1. f-strings (Fastest & Most Readable) msg = f"User {name.upper():>10} reached Level {level:03d}" # 2. Join (The only way to build large strings efficiently) parts = ["System", "Status", "Operational"] banner = " | ".join(parts) # 3. Essential Manipulation raw_data = " \n CODE-123-X \t " clean_code = raw_data.strip().lower().replace("-", "_") # 4. Slicing (Powerful & Pythonic) # [start:stop:step] reversed_str = clean_code[::-1]
β οΈ Interview Pitfalls
- The Concatenation Trap: Always mentioned in performance interviews. If you need to build a string from 1,000 components, never use
+=. - Immutable Gotcha: You cannot do
s[0] = 'a'. You must slice and rebuild:s = 'a' + s[1:]. isvs==for strings: While small strings might returnTruefors1 is s2due to interning, it is not guaranteed. Always use==for value comparison.- Encoding: Know the difference between a
str(Unicode) andbytes(encoded).