Python strings come equipped with a powerful suite of built-in methods. Mastering these is key to idiomatic Python code.
1. Splitting and Joining
The split() method breaks a string into a list based on a delimiter, while join() performs the inverse operation, which is much more efficient than string concatenation in a loop.
2. Cleaning and Replacing
Use strip() to remove whitespace (or specific characters) from both ends. replace() allows for global substitutions within the string.
3. Prefix and Suffix Checks
Instead of manual slicing, use startswith() and endswith(). They also accept tuples of strings to check against multiple possibilities.
4. Unicode and Character Codes
Python 3 strings are Unicode by default. You can convert characters to their integer code points using ord() and back with chr().