What Is a Method in Python?
A method in Python is a function that belongs to an object.
You use methods all the time in Python, even in simple code like:
name = "Ada"
print(name.upper())
In this example, upper() is a method of a string object.
Understanding methods helps you read Python code more easily. It also helps you know why some actions are written as object.method() while others are written as regular functions like len(object).
Definition
A method is a function that belongs to an object.
Key points:
- A method is called on an object
- You use dot notation to call it
- The usual shape is
object.method() - Methods can perform an action or return information
Example:
text = " hello "
cleaned = text.strip()
print(cleaned)
Output:
hello
Here:
textis the objectstripis the method name()runs the method
The strip() method removes spaces from the beginning and end of the string.
If you are new to objects, see what an object is in Python.
How methods are different from functions
A method is similar to a function, but they are not called in the same way.
- A function is called by name, like
len(my_list) - A method is called on an object, like
my_list.append(5) - Methods belong to a specific type or class
- Not every operation in Python uses a method
Example:
numbers = [1, 2, 3]
print(len(numbers)) # function
numbers.append(4) # method
print(numbers)
Output:
3
[1, 2, 3, 4]
In this example:
len(numbers)uses the built-in functionlennumbers.append(4)uses the list methodappend
So even though both work with the list, one is a function and one is a method.
For a practical example, see the Python list append() method.
Simple examples of methods
Different object types have different methods.
String methods
Strings have methods such as:
lower()upper()strip()
Example:
word = " PyThOn "
print(word.lower())
print(word.upper())
print(word.strip())
Output:
python
PYTHON
PyThOn
If you want to learn one of these in more detail, see the Python string lower() method.
List methods
Lists have methods such as:
append()pop()sort()
Example:
numbers = [3, 1, 2]
numbers.append(4)
numbers.sort()
print(numbers)
print(numbers.pop())
print(numbers)
Output:
[1, 2, 3, 4]
4
[1, 2, 3]
Dictionary methods
Dictionaries have methods such as:
get()keys()items()
Example:
user = {"name": "Sam", "age": 20}
print(user.get("name"))
print(user.keys())
print(user.items())
Output:
Sam
dict_keys(['name', 'age'])
dict_items([('name', 20), ('age', 20)])
These methods work because each type provides its own set of methods.
How to read method calls
When you see code like this:
name = "maria"
print(name.upper())
You can read it like this:
nameis the objectupperis the method name()calls the method
Some methods also take arguments.
Example:
text = "banana"
new_text = text.replace("a", "o")
print(new_text)
Output:
bonono
Here:
textis the objectreplaceis the method"a"and"o"are arguments passed into the method
A good way to read it is:
“Call the
replacemethod ontext.”
Methods in classes
You can also create your own methods by defining them inside a class.
If you have not learned this yet, see Python classes and objects explained and what a class is in Python.
Example:
class Dog:
def bark(self):
return "Woof!"
my_dog = Dog()
print(my_dog.bark())
Output:
Woof!
In this example:
Dogis a classmy_dogis an object made from that classbark()is a method of theDogclass
What self means
A method in a class usually has self as its first parameter:
class Person:
def greet(self):
return "Hello"
p = Person()
print(p.greet())
Output:
Hello
self refers to the current object.
You do not pass self yourself when calling the method. Python does that for you.
Here is another example:
class Counter:
def __init__(self):
self.value = 0
def increase(self):
self.value += 1
counter = Counter()
counter.increase()
counter.increase()
print(counter.value)
Output:
2
The increase() method changes data stored in the object.
What beginners often confuse
Beginners often run into the same problems when learning methods.
Forgetting parentheses
A method call usually needs parentheses.
Example:
text = "hello"
print(text.upper) # no parentheses
print(text.upper()) # correct
Output:
<built-in method upper of str object at ...>
HELLO
Without parentheses, you are referring to the method itself, not calling it.
Using the wrong method on the wrong type
Not all types have the same methods.
Example:
numbers = [1, 2, 3]
print(numbers.lower())
This causes an error because lists do not have a lower() method.
You would get an AttributeError. If you see that problem, read how to fix AttributeError: object has no attribute.
Confusing functions with methods
These are different:
text = "hello"
print(len(text)) # function
print(text.upper()) # method
Both are valid, but they are called differently.
Not understanding that methods belong to objects
If you are not sure what kind of object you have, these tools help:
value = "hello"
print(type(value))
print(dir(value))
help(value.upper)
Useful debugging commands:
type(value)shows the typedir(value)shows available attributes and methodshelp(value.method_name)shows documentation for a specific method
FAQ
Is a method the same as a function in Python?
Not exactly. A method is a function attached to an object or class.
How do I know if something is a method?
If you call it on an object with dot notation, like text.lower(), it is a method.
Why does Python use both functions and methods?
Some actions make more sense as object behavior, while others work as general-purpose functions.
What does self mean in a method?
self refers to the current object and is used inside class methods.
Can I create my own methods?
Yes. When you define functions inside a class, they become methods of that class.