Part One
Functions vs. Methods
You have already used print() and len(). These are functions — standalone commands that you call by writing their name followed by parentheses, with an input inside: len("hello").
A method is different: it belongs to a specific piece of data and is called using dot notation — the data first, then a dot, then the method name: "hello".upper(). Think of a method as something the data knows how to do to itself.
something.method() — it's a method. If the data goes inside the parentheses — function(something) — it's a function.
Part Two
Built-in Functions
Python ships with dozens of functions that are always available. Here are the ones you will reach for most often.
len() — the length of things
len() returns the number of items in a list, or the number of characters in a string.
type() — what kind of thing is this?
type() tells you what data type a variable holds. Useful when something isn't behaving as expected.
int(), float(), str() — converting between types
These functions convert a value from one type to another. This is essential when, for example, you read a number that is stored as text and need to do arithmetic with it.
round() — rounding numbers
round(number, digits) rounds a number to the given number of decimal places. If you omit the second argument it rounds to the nearest whole number.
max() and min() — largest and smallest
These work on a list of numbers (or strings) and return the largest or smallest value.
sum() — total of a list
Part Three
String Methods
Strings in Python come loaded with methods. Because strings are so common in real programs — names, addresses, messages, file contents — these methods save enormous amounts of work.
.upper() and .lower() — changing case
.title() — title case text
.title() makes the first letter of each word uppercase and the rest lowercase.
.strip() — removing extra whitespace
Real-world data often has unwanted spaces at the start or end. .strip() removes them.
.replace() — substituting text
.replace(old, new) returns a new string with every occurrence of old swapped for new.
.split() — breaking a string into a list
.split(separator) cuts the string wherever it finds the separator and returns a list of the pieces. If you omit the separator, it splits on any whitespace.
.startswith() and .endswith() — checking beginnings and endings
These return True or False, making them ideal for use inside if statements.
.count() — counting occurrences
Slicing strings — taking parts of text
You can extract part of a string with slicing: text[start:end]. It starts at start and stops before end.
.camel() — making camelCase (custom helper)
Python does not include a built-in .camel() string method, but you can make the same result with a small helper function.
clean = name.strip().
Part Four
List Methods
Lists also have their own set of methods. Unlike string methods, several list methods modify the list in place — they change the original directly, rather than returning a new one.
.append() — adding an item
You have seen this before. It adds one item to the end of the list.
.sort() — sorting in place
.sort() rearranges the list alphabetically (for strings) or numerically (for numbers). It modifies the list directly.
.count() — counting occurrences in a list
Removing items — .remove(), .pop(), and del
There are different ways to remove from a list. .remove(value) removes by value, .pop(index) removes by position and returns the removed item, and del deletes by index.
Putting it together
.sort() and .append() modify the list directly and return nothing. .upper(), .strip(), .replace() leave the original unchanged and return a new value. When in doubt, print the result to see what you got.
Chapter Navigation
Move between chapters.