Methods or Dot Functions

A data type defines what operations can be used on that type and what methods or dot functions exist for objects of that type. These concepts will be covered more deeply near the end of the book, but for now know that this essentially means that the designers of Python designed functions for every data type to be manipulated. We will focus on the string and the list data types. We will use the built in functionality that Python offers to manipulate these data types.

Dot Function Syntax

A dot function is a function that operates on an object of a specific type or class. They are also referred to as methods. The syntax of calling a dot function is appending a dot to the end of an object followed by the function name and a parenthetical:

object.functionName()

The function takes the object as an input, and outputs something based on the input and the function's definition. Remember that the function must be for the data type of the object. Again, we will be using methods that are built into Python by its developers for use on its data types.

Strings

Concatenation

The + operator not only works for numbers but also strings. When two strings are "added", the string on the right will be appended to the one on the left. This is known as concatenation.

For example:

>>> x = "hello"
>>> y = " world! "
>>> x+y
'hello world! '

Here the string y is appended to the string x.

Capitalization

Now say I need to capitalize every word of this sentence. We can use the .title() function to accomplish this.

>>> x = "hello"
>>> y = " world! "
>>> (x+y).title()
'Hello World! '

Strip Whitespace

Now say I want to remove the whitespace I left at the end of the string. Whitespace is any space that isn't text such as a Space or Tab. The .strip() function does this.

>>> x = "hello"
>>> y = " world! "
>>> (x+y).title().strip()
'Hello World!'

Note that dot functions can be listed one after the other. They work in order from outside in. In other words, from right to left.

>>> x = "hello"
>>> y = " world! "
>>> z = x + y
>>> z = z.strip()
>>> z = z.title()
>>> z
'Hello World!'

Here the same is accomplished by assigning x+y to z and using the functions on z one at a time. The main difference between these two ways is that some would say one is more readable by a human than the other. Depending on the context this may be true.

Lists

Python contains numerous built in methods for manipulating lists. Say I have a list of numbers 1 - 10:

>>> myList = [1,2,3,4,5,6,7,8,9,10]

Append

The .append() method will add a new object to the end of a list. I will continue the pattern and add 11.

>>> myList = [1,2,3,4,5,6,7,8,9,10]
>>> myList.append(11)
>>> myList
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Pop

The .pop method will remove or "pop" the first last object out of the list.

>>> myList.pop()
11
>>> myList
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Reverse

The .reverse() method reverses the order of all elements in a list.

>>> myList.reverse()
>>> myList
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

This section is just a small taste of the different built-in methods Python has to offer. Every data type has a number of methods available for use. The list of methods used and discussed in this book is by no means exhaustive. Refer to the Python documentation for more information. Links to some of the documentation are below.

Further Reading

Python Docs on List Methods

Python Docs on String Methods