Python — strings and text processing
Be able to process text with string methods, formatting and simple parsing.
Prerequisites
- CPython — the basicsrequired
Intuition
Almost all the data a language model meets is text. Being able to process text in Python is therefore fundamental.
A string is a sequence of characters. You can:
- index and slice it:
s[0],s[-1],s[2:5] - normalise it:
s.lower(),s.strip() - split and join:
s.split()," ".join(items) - replace:
s.replace("a", "b") - ask questions:
"ai" in s,s.startswith("Hi")
Strings are immutable: the methods give you a new string, the original is unchanged.
Code
line = " Anna, 14, Gothenburg "
parts = [p.strip() for p in line.strip().split(",")]
print(parts) # ['Anna', '14', 'Gothenburg']
name, age, city = parts
print(f"{name} is {int(age) + 1} next year") # Anna is 15 next year
text = "AI is fun. AI is hard."
print(text.lower().count("ai")) # 2
words = text.lower().replace(".", "").split()
print(len(words), words[:3]) # 6 ['ai', 'is', 'fun']
Tokenisation in a language model starts exactly like this: normalise, split, count.
Mastery means
- Uses the string methods (lower, split, strip, replace) correctly
- Builds strings with f-strings
- Parses simple text into structure
Sign in to do the exercises and build your mastery up.
Sources
Leads to
Part of the goals (17)
- Build a RAG system you can trust
- Data: collect, clean, document
- The developer's toolbox
- Language models in practice
- Build a memory system for an agent
- Classical machine learning in practice
- Build an agent you can trust
- Foundations of computer science
- Systems knowledge for AI engineers
- Classical ML for real
- Fine-tune and run your own models
- AI in production
- Build an AI service that survives production
- Build an NLP system end to end
- AI safety in practice
- An AI service in operation
- Training neural networks for real