Input and Output Built-ins

Built-in Functions

Input and Output Built-ins

In this tutorial, we'll use print() and input() in simple interactive programs.

Two beginner-friendly built-ins are print() and input().

print()

print() displays output:

print("Hello")
print("Score:", 95)

It is useful for user messages and debugging.

input()

input() asks the user for text:

name = input("What is your name? ")
print(f"Hello, {name}")

The prompt is the text shown to the user.

input() Always Returns a String

Even if the user types a number, Python receives text:

age = input("Age: ")
print(type(age))

To do maths, convert it:

age = int(input("Age: "))
print(f"Next year you will be {age + 1}")

Safer Input

User input can be invalid. Later, exception handling helps:

try:
    age = int(input("Age: "))
    print(age + 1)
except ValueError:
    print("Please enter a whole number")

Keep Prompts Clear

A good prompt tells the user what to type:

age = input("Enter your age in years: ")

This is clearer than:

age = input("Age: ")

Small wording choices make interactive programs easier to use.

input() Pauses the Program

When Python reaches input(), it waits. The next line does not run until the user types something and presses Enter.

That means input is useful for interactive programs, but not for code that should run without waiting for a person.

Practice

Ask the user for their name and favourite number. Convert the number and print double it.