Skip to main content

How to Call a Method on an Object in Python

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Every value in Python comes with functions of its own, called methods. Text has a method that capitalises every word, another that swaps one word for another, and many more. You call one with a dot after the value, the method's name and a pair of parentheses. This guide calls two string methods on Python 3.15.2, one that needs nothing and one that needs two values, and shows what goes wrong without the parentheses.

Syntax​

Syntax
<result> = <object>.<method>(<arguments>)
PartWhat it means
<object>The value the method belongs to, or a variable that holds it
.Reaches into the object for one of its methods
<method>The method's name
(<arguments>)The parentheses run the method. Put the values it needs inside them, separated by commas, or leave them empty when it needs none
<result>What the method gives back. You can keep it in a variable, or use it straight away, for example inside print()

Python's glossary defines a method as "A function which is defined inside a class body". A class is the blueprint of a type, such as str for text, so every string gets the methods written in str. The tutorial shows the usual way to use one: "a method is called right after it is bound: x.f()".

The Example​

A variable called greeting holds the text hello world. The script calls title(), which needs no arguments, and replace("world", "Python"), which needs two, then prints greeting itself.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

methods.py
greeting = "hello world"

print(greeting.title())
print(greeting.replace("world", "Python"))
print(greeting)

The methods.py script in a macOS-style code window: greeting is set to hello world, then the script prints greeting.title(), greeting.replace("world", "Python") and greeting.

greeting.title() calls the title method of the text in greeting, with nothing between the parentheses. greeting.replace("world", "Python") calls replace with two arguments: the text to look for and the text to put in its place.

Step 2: Run It​

Terminal
uv run python methods.py
Output
Hello World
hello Python
hello world

The terminal after uv run python methods.py: Hello World, then hello Python, then hello world.

title() returned Hello World; the documentation says str.title() returns a version "where words start with an uppercase character and the remaining characters are lowercase". replace() returned hello Python, because str.replace() returns "a copy of the string with all occurrences of substring old replaced by new". The last line shows that greeting is still hello world: both methods built new text and left the original alone. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving off the parentheses: print(greeting.title)Python prints the method instead of running it: <built-in method title of str object at 0x102dd63f0>, with a different address after at on each runAdd the parentheses: greeting.title()
Misspelling the method: greeting.titel()AttributeError: 'str' object has no attribute 'titel'. Did you mean '.title' instead of '.titel'?Use the spelling Python suggests
Calling a method the value doesn't have: visitors.title() after visitors = 4AttributeError: 'int' object has no attribute 'title'Each type has its own methods. title() belongs to text, and 4 is a whole number, an int

FAQ​

What's the difference between a function and a method?​

A function stands on its own and takes the value inside its parentheses. A method belongs to the value and comes after a dot. On the same greeting, the function len(greeting) printed 11, the number of characters, and the method greeting.title() printed Hello World.

Does calling a method change the variable?​

It depends on the type. Text can't be changed in place, so string methods always return new text, as greeting showed above. Other types have methods that change the object itself: after drinks = ["tea"] and drinks.append("coffee"), print(drinks) printed ['tea', 'coffee']. The documentation describes append as "Append value to the end of the sequence."

Conclusion​

greeting.title() returned Hello World and greeting.replace("world", "Python") returned hello Python, while greeting stayed hello world. A method call is the object, a dot, the method's name and parentheses, with any arguments inside them. Without the parentheses, Python shows the method instead of running it.

Additional Resources​

  • Method objects — the tutorial on how methods are called.
  • method — the glossary's definition.
  • String methods — every method a Python string has.
  • Calls — the reference for the parentheses that run a function or method.