Operators, Functions and Methods

Last updated on 2026-10-19 | Edit this page

Estimated time: 25 minutes

Overview

Questions

  • What can I do with values of different types?
  • What happens when I combine values of different types?
  • What are functions and methods?

Objectives

  • Use arithmetic operators on numbers and strings.
  • Read and interpret error messages.
  • Convert values from one type to another.
  • Call built-in functions and string methods.

We continue with the variables from the previous episode:

PYTHON

name = "Lovelace"
age = 37
size = 1.7123

Operators


An object’s type determines what the program can do with it.

PYTHON

# Addition of integers
5 + 5

OUTPUT

10

PYTHON

# Multiplication
age * 100

OUTPUT

3700

The same operators also work on strings, but they do something different:

PYTHON

# Concatenation of strings
name + name

OUTPUT

'LovelaceLovelace'

PYTHON

# Concatenation of strings x times
name * 10

OUTPUT

'LovelaceLovelaceLovelaceLovelaceLovelaceLovelaceLovelaceLovelaceLovelaceLovelace'

We get an error if we try to add a number to a string.

PYTHON

# Does not work: the "+" operator cannot be applied
name + 10

ERROR

TypeError: can only concatenate str (not "int") to str

PYTHON

# Works: here, "10" is a string
name + "10"

OUTPUT

'Lovelace10'
Callout

Reading error messages

Error messages can look intimidating, but they are there to help you. Read the last line first: it tells you the type of the error (here a TypeError) and a short description of what went wrong. Above it, Python shows you the line of code where the error happened.

Dividing two numbers always results in a float:

PYTHON

# Division
size / age

OUTPUT

0.04627837837837838

PYTHON

type(size)

OUTPUT

float

Type conversion


Data that we read from files or from the web often arrives as text, even if it looks like a number.

PYTHON

number_of_articles = "50"

PYTHON

# Does not work: unsupported operand type(s) for /: 'str' and 'int'
number_of_articles / 10

ERROR

TypeError: unsupported operand type(s) for /: 'str' and 'int'

PYTHON

# First convert the string to an integer using int()
int(number_of_articles) / 10

OUTPUT

5.0

Similarly, str() converts a value to a string and float() converts it to a floating point number.

Functions and methods


The len() function tells us the length of an item. In the case of a string, for example, it tells us how many characters it contains.

PYTHON

len(name)

OUTPUT

8

Methods are special types of function that are always bound to an object, the thing on the left of the dot. For now, the difference between a function and a method is not that relevant.

PYTHON

name.upper()

OUTPUT

'LOVELACE'

PYTHON

# The string itself is not changed
name

OUTPUT

'Lovelace'

PYTHON

name.count("e")

OUTPUT

2

PYTHON

# Methods with two arguments
name.replace("lace", " is everywhere")

OUTPUT

'Love is everywhere'
Callout

Getting help

Use the built-in function help() to see the documentation of a function or method, e.g. help(len) or help(name.replace). In Jupyter, you can also put the cursor inside the parentheses and press Shift+Tab.

Challenge

Fix the error

A colleague wants to print how many articles a library has licensed, but the code below fails. What is wrong, and how can you fix it?

PYTHON

number_of_articles = 50
print("We licensed " + number_of_articles + " articles.")

number_of_articles is an int, and Python cannot add an int to a str. Convert the number to a string first:

PYTHON

print("We licensed " + str(number_of_articles) + " articles.")

OUTPUT

We licensed 50 articles.
Challenge

String methods

Given title = "introduction to python", use string methods to

  1. print the title in capital letters,
  2. count how many times the letter "o" occurs,
  3. replace "python" with "pandas".

PYTHON

title = "introduction to python"
print(title.upper())
print(title.count("o"))
print(title.replace("python", "pandas"))

OUTPUT

INTRODUCTION TO PYTHON
4
introduction to pandas
Key Points
  • The type of a value determines what an operator does with it.
  • Read error messages from the bottom up: the last line names the error.
  • Use int(), float() and str() to convert between types.
  • Functions are called with function(arguments), methods with object.method(arguments).
  • String methods return a new value and do not change the original string.