Split, join, and search
Real text usually comes in chunks: words in a sentence, fields in a CSV line, tags separated by commas. Python gives you .split() to break text apart and .join() to put pieces back together — plus a few methods to test how text begins and ends.
Splitting text into a list
.split() breaks a string into a list of pieces. With no argument it splits on whitespace:
Python
Pass a separator to split on something else:
Python
Joining a list into text
.join() is the opposite of split: it glues a list of strings together. You call it on the separator string and pass the list:
Python
A common pattern is split, transform, then join:
Python
Checking how text starts or ends
These return True/False and are great for filtering:
Python
Counting occurrences
.count() tells you how many times a substring appears:
Python
Key takeaways
.split()turns a string into a list (whitespace by default, or a separator you pass)..join()turns a list back into a string:separator.join(list)..startswith()/.endswith()test the beginning/end and return booleans..count()counts how many times a substring appears.
No comments yet. Add the first comment to start the discussion.