• 6 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 2/18/22

Call functions using methods

Log in or subscribe for free to enjoy all this course has to offer!

What is a method?

You already know a little about functions, which are blocks of code with a name that you can reuse. That block of code is a set of instructions you want a program to perform.

But what are methods?

Methods are functions that are defined inside a class. You can, however, isolate instance methods, that are executed within the context of an object from class methods, which operate directly on the class.

That sounds a bit abstract, but you have already been using them! Let's review some data types:

String methods

Python has quite a few methods that string objects can call to perform frequently occurring tasks related to strings. For example, you may need to put your string in capital letters, or maybe just the first character, or replace a specific character.

Here are the corresponding methods:

  • upper: returns an uppercase string.

  • lower: returns a lowercase string.

  • capitalize: capitalizes the first letter. 

  • count: returns occurrences of substring in string.

  • replace: replaces a character or a substring inside your string.

Let's try this with Python:

some strings methods
Some strings methods - Click to enlarge!

You can find the full list of methods by using the help function over the string object: help(str).

Methods work exactly like functions: they can have arguments and return value. All string methods return new values. They do not change the original string.

To call a method, use a dot notation, which is common in many programming languages. Write the variable's name followed by the method name, separated by a dot:   variableName.methodName() .

Collections methods

There are a lot of list or dict methods you already know. For example,  append,  insert , and remove are the most commonly used. Let's check out some others:

  • sort : sorts the list (from min to max with numerics, alphabetical order with strings). You can't sort a list of mixed data types.

  • pop : removes the element at the specified position, and displays it for the list. It also removes the element with the specified key and displays it for the dict.

  • values : returns a list of all the values in the dictionary.

For instance:

myList = [1, 2, 1000, 289, 40.2, -20]
myDict = {'apple': 4, 'orange': 2, 'strawberry':10}
myList.sort() # sort and replace the list, not returning any value
print(myList) # -> -20, 1, 2, 40.2, 289, 1000
print(myList) # -> -20, 1, 2, 289, 1000
print(myDict) # -> only orange and strawberry
mySecondList = myDict.values()
print(mySecondList) # -> [2, 10]


In this chapter, you've learned:

  • Methods of a class are functions that operate in the context of that class.

  • To call methods, a dot notation is commonly used: variableName.methodName() or lassName.methodName().

  • Some methods change the original object, while others return a new object with the result of the method applied.

Example of certificate of achievement
Example of certificate of achievement