01-print-and-input.py
python-for-j277 / getting-started  ·  Lesson 01
Lesson 01 — Getting started

Print & Input

Show things on the screen and ask the user for answers.

Every program needs two everyday skills: showing information and collecting it. In Python these are the functions print() and input() — and they are where everyone starts.

Showing text with print()

print() displays whatever you put inside its round brackets.

hello.py
print("Hello, world!")
print("Welcome to Python.")
Output
Hello, world!
Welcome to Python.
How it works
  • print() is a built-in command that shows whatever is inside its brackets.
  • The text inside the quotation marks is a string — it appears exactly as written.
  • Each print() starts a new line, so the two messages sit one above the other.

Printing several things at once

score.py
print("Your score is", 10, "out of", 10)
Output
Your score is 10 out of 10
How it works
  • List several items inside print(), separated by commas.
  • Python puts a single space between each item for you.
  • Items can be text or numbers — here we mix both.

Storing answers in variables

A variable is a named box that holds a value so you can use it later.

name.py
name = "Sam"
print("Hello", name)
Output
Hello Sam
How it works
  • name is a variable — a named box that stores a value.
  • The = symbol means 'put the value on the right into the box on the left'.
  • When you use name later, Python swaps in whatever it is holding.

Asking the user a question with input()

greeting.py
name = input("What is your name? ")
print("Nice to meet you,", name)
Output
What is your name? Sam
Nice to meet you, Sam
How it works
  • input() shows the message in the brackets, then waits for the user to type and press Enter.
  • Whatever they type is handed back and stored in the variable name.
  • We then use that variable inside print().
Mind the gapA space at the end of the prompt — "What is your name? " — stops the user's typing sticking to the question mark.
Input is always textinput() always gives back a string, even if the user types a number. You will learn how to convert it on the next page.

What you have learned

  • print() displays text and values on the screen.
  • Separate several items with commas; Python adds the spaces.
  • A variable is a named box that stores a value using =.
  • input() asks a question and returns whatever the user types, as text.