Python Arena
Step 3 of 4

Functions, arguments and return values

A function is a named piece of functionality that a program can use. You have used several already: print, input, int, float, str and type. Using a function is called calling it: you write its name followed by parentheses.

The values inside the parentheses are the function's arguments, the data it works on. In print("Score:", score) the two arguments are "Score:" and the value of score. Some functions can be called with no arguments at all (print() prints an empty line), and some take several, separated by commas. In everyday talk, programmers often say parameter when they mean an argument. Strictly, a parameter is the name inside a function that receives an argument, a difference you'll see once you write functions of your own later in the course.

Return values

Many functions also give back a value, called their return value. When the call runs, Python puts the return value in its place, so a function call is an expression. input returns what the user typed, as a string, which is why you usually store it in a variable.

A new function shows this well. len returns the length of a string: the number of characters in it, with spaces and punctuation included.

sentence = input("Type a sentence: ")
print(f"That's {len(sentence)} characters.")
Input
I like tea.
Output
Type a sentence: I like tea.
That's 11 characters.

The letters make up 8 of the 11 characters; the two spaces and the period are the other 3. If the user presses Enter without typing anything, input returns the empty string "", a string with no characters at all, and its length is 0. Since len(...) gives an int, you can store it, calculate with it or compare it, like any other number:

name = input("Choose a username: ")
length = len(name)
if length < 5:
    print(f"{name} has only {length} characters. Use at least 5.")
Input
ada
Output
Choose a username: ada
ada has only 3 characters. Use at least 5.

Calls inside calls

Since a call is an expression, it can be the argument of another call. You have done this since part 1: in int(input("Age: ")), Python first calls input, and its return value, a string, becomes the argument of int. The conversion functions int, float and str are functions like any other: each takes one argument and returns its value as another type.

Not every argument works with every function. len counts the characters of a string, but a number has no characters to count:

year = 2026
print(len(year))
Python stops with
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    print(len(year))
          ~~~^^^^^^
TypeError: object of type 'int' has no len()

The message says that a value of the type int has no length. Convert the number into a string first, and then count: len(str(year)) is 4, since str(year) is the string "2026".

Your task

Challenge 1 of 3

Fill in the blank with a function call, so the program prints how many characters the word has:

Type a word: keyboard
The word keyboard has 8 characters.

Challenge 2

The starter code reads a whole number with int. Add a line that prints how many digits the number has. Calling len with the number itself stops with an error, so convert it first.

Type a whole number: 2026
The number 2026 has 4 digits.

Challenge 3

A status update can be at most 30 characters long. Write a program that asks for a status, prints how many characters it has, and if it has more than 30, also prints how many characters too long it is.

Status: Learning Python one step at a time
34 characters
Too long by 4 characters.