Python 101Free
FOUNDATIONS

Input and Output

Talking to the user with print and input, and how a script actually runs.

SECTION 01

print and input basics

print writes its arguments to standard output, separated by spaces, ending with a newline. The sep argument changes the separator and end changes the trailing character. Pass several arguments and Python handles spacing between them automatically.

input is the reverse. It blocks until the user types something and presses enter, then returns whatever they typed as a string. The optional argument is a prompt that gets printed first.

The most common surprise is that input always returns a string, even when the user types digits. If you want a number, you have to convert it: int(input("age? ")). Forgetting this conversion is a classic source of TypeError later in the code.

python
print("hi", "there", sep="-", end="!\n")
# hi-there!

age = int(input("age? "))   # cast right away; input is always str
1 more section

Create a free account to access all sections and animations.

Create free accountAlready have an account? Sign in