Python Strings – Topic-Wise Notes
1. Introduction to Strings
Strings are sequences of characters enclosed in single ('...') or double ("...") quotes.
Triple quotes ('''...''' or """...""") are used for multi-line strings.
2. String Indexing and Slicing
Indexing allows accessing individual characters using positive or negative indices.
Slicing extracts parts of strings using syntax s[start:end:step].
3. String Operations
Concatenation: use '+' to combine strings.
Repetition: use '*' to repeat strings.
Membership testing: use 'in' or 'not in'.
4. String Methods
Case conversion: upper(), lower(), capitalize().
Searching: find(), replace().
Splitting/Joining: split(), join().
Trimming: strip(), lstrip(), rstrip().
5. String Formatting
Use format() for traditional formatting.
Use f-strings (f'...') for concise formatting.
6. Escape Characters
\n for newline, \t for tab, \' for single quote, \\ for backslash.
7. String Comparison
Use comparison operators (==, !=, <, >) to compare strings lexicographically.
8. String Immutability
Strings cannot be changed after creation. Use slicing to create modified copies.
9. String Methods Overview
len(), isalpha(), isdigit(), startswith(), endswith() are common methods.
10. String Iteration
Iterate over characters in a string using a for-loop.
11. String Testing Methods
isalnum(), isalpha(), isdigit(), islower(), isupper() test string content.
12. String Identity and Interning
Python reuses string objects with the same value for efficiency.
13. String Encoding and Decoding
Use encode() to convert to bytes, decode() to convert back to string.
14. String Joining and Splitting
join() combines list elements into a string, split() breaks a string into a list.
15. String Formatting with Dictionaries
Use format(**dict) to plug in dictionary values into strings.
16. String Translation and Mapping
Use [Link]() with translate() to replace multiple characters.
17. String Partitioning
partition() splits string into a 3-tuple at the first occurrence of a separator.
18. String Centering and Alignment
Use center(), ljust(), rjust() to align strings within a given width.
19. String Casefolding
casefold() provides aggressive lowercasing for caseless matching.
20. String Zfill
zfill(width) pads string on the left with zeros to fill the width.