Skip to content

C3 · Built-in Functions

Spec reference: Section C - Programming Paradigms
Key idea: Use arithmetic, string and general built-in functions to process data.


Variables and Data Types in Python


What are built-in functions?

Built-in functions are pre-written functions provided by a programming language that perform common operations. You call them by name without needing to write the underlying code yourself.

They fall into three categories: arithmetic, string handling, and general.


Arithmetic functions

FunctionWhat it doesExampleResult
ROUND(x, n)Round x to n decimal placesROUND(3.7654, 2)3.77
INT(x) / int(x)Truncate to integer (remove decimal)INT(7.9)7
ABS(x)Absolute value (remove negative sign)ABS(-5)5
SQRT(x)Square rootSQRT(16)4.0
MAX(a, b, ...)Largest of the values givenMAX(3, 9, 1)9
MIN(a, b, ...)Smallest of the values givenMIN(3, 9, 1)1
python
import math

print(round(3.7654, 2))    # 3.77
print(int(7.9))            # 7
print(abs(-15))            # 15
print(math.sqrt(25))       # 5.0
print(max(4, 9, 2))        # 9

String handling functions

Strings are sequences of characters. String functions let you inspect, extract, and modify text.

FunctionWhat it doesExampleResult
LEN(s)Number of charactersLEN("Hello")5
UPPER(s)Convert to uppercaseUPPER("hello")"HELLO"
LOWER(s)Convert to lowercaseLOWER("HELLO")"hello"
SUBSTRING(s, start, len)Extract part of a stringSUBSTRING("Hello", 1, 3)"Hel"
LEFT(s, n)First n charactersLEFT("Hello", 3)"Hel"
RIGHT(s, n)Last n charactersRIGHT("Hello", 3)"llo"
CONTAINS(s, t)Does s contain t?CONTAINS("Hello", "ell")True
python
name = "Hello, World!"

print(len(name))             # 13
print(name.upper())          # HELLO, WORLD!
print(name.lower())          # hello, world!
print(name[0:5])             # Hello   (substring)
print("World" in name)       # True    (contains)
print(name.replace("World", "Python"))  # Hello, Python!

String indexing

Strings are zero-indexed in most languages:

H  e  l  l  o
0  1  2  3  4

So "Hello"[0] = 'H' and "Hello"[4] = 'o'.


General functions

FunctionWhat it doesExampleResult
STR(x)Convert to stringSTR(42)"42"
INT(x)Convert to integerINT("42")42
FLOAT(x)Convert to floatFLOAT("3.14")3.14
INPUT(prompt)Get text input from userINPUT("Enter name: ")user's text
RANDOM(a, b)Random integer between a and bRANDOM(1, 6)1 to 6
python
import random

user_age = int(input("Enter your age: "))  # input() + INT conversion
dice = random.randint(1, 6)
print("You rolled:", dice)

Worked example: Name formatter

python
def format_name(first, last):
    # Capitalise first letter of each, combine
    first = first.strip().capitalize()
    last = last.strip().capitalize()
    full = first + " " + last
    initials = first[0] + last[0]
    return full, initials

name, init = format_name("alice", "smith")
print(name)     # Alice Smith
print(init)     # AS

Test Yourself

Question 1 of 5

What does LEN("Computing") return?

Ad

PassMaven - revision made simple.