Level 1
0 / 100 XP

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
sentence = "the quick brown fox" words = sentence.split() print(words) # ['the', 'quick', 'brown', 'fox'] print(len(words)) # 4

Pass a separator to split on something else:

Python
csv = "name,age,city" fields = csv.split(",") print(fields) # ['name', 'age', 'city']

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
words = ["the", "quick", "brown", "fox"] print(" ".join(words)) # the quick brown fox print("-".join(words)) # the-quick-brown-fox

A common pattern is split, transform, then join:

Python
csv = "name,age,city" print(" | ".join(csv.split(","))) # name | age | city

Checking how text starts or ends

These return True/False and are great for filtering:

Python
filename = "report.pdf" print(filename.startswith("report")) # True print(filename.endswith(".pdf")) # True print(filename.endswith(".txt")) # False

Counting occurrences

.count() tells you how many times a substring appears:

Python
text = "banana" print(text.count("a")) # 3 print(text.count("na")) # 2

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.