Module 1 · Python crash course · Lesson 8 of 66
Methods on strings, lists and dictionaries
Built-in tools like split, lower, pop, keys and in.
In this lesson · about 12 minutes
- Call methods with a dot
- Split and clean up text
- Check membership with in
A method is a function that belongs to a value, called with a dot: text.lower(). Some you'll use all the time:
| Code | What it does |
|---|---|
s.lower(), s.upper() | Change case |
s.split() | Break text into a list of words (on spaces) |
s.split('#') | Break on any character you choose |
d.keys(), d.items() | A dictionary's keys, or its (key, value) pairs |
lst.pop() | Remove and return the last item |
'x' in lst | True if the value is in the list (or text) |
Exercise
Split a sentence
Split s into a list of words.
Exercise
Is there a dog?
Write findDog(st) that returns True if the word "dog" is in the string, whatever its capitalization. Don't worry about punctuation stuck to the word.
Exercise
Count the dogs
Write countDog(st) that counts how many times the word "dog" appears.
Exercises solved: 0 of 3
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.