input() and Output

Reading a line from the user, why it is always text, and the print options worth knowing.

Overview

A note about this page

The editors here run Python in your browser, which has no keyboard attached to standard input, so input() raises OSError. The first program calls it inside a try so you can see exactly that, rather than the page pretending otherwise. Everything else uses a stand-in value, and behaves identically to what you would get from a real terminal.

input_basics.py

input_basics.py Python 3
Output

                    

print_options.py

print_options.py Python 3
Output

                    

Worth knowing

input() always returns a string, even when the user types digits.
It needs a keyboard, so it raises in this in-browser runtime. On your machine it waits for a line.
print(a, b, sep="-") changes the separator; end="" suppresses the newline.
Convert with int() inside a try, because the user can type anything.

input() and Output: A Practical Guide

input() reads one line from the user and returns it as a string — always a string, whatever it looks like. print() sends values the other way, with two options worth knowing.

It is always text

typed = input("Age: ")  # user types 42
typed + typed          # "4242", not 84

This is the first surprise everyone meets. input cannot know whether "42" is meant as a number, a house number or a password, so it does not guess. Convert explicitly:

age = int(input("Age: "))

Convert defensively

That one-liner raises ValueError the moment somebody types "forty" or presses enter on an empty line. For anything a real person will use:

def read_int(text, default=0):
    try:
        return int(text.strip())
    except (ValueError, AttributeError):
        return default

strip() first, because people type spaces.

The prompt is an argument

input("Your name: ") prints the prompt and reads on the same line. A separate print before it works too but puts the cursor on the next line, which reads worse.

print has two useful options

print("a", "b", sep="-")    # a-b
print(i, end=" ")         # no newline

sep sits between the values; the default is a single space. end goes after them; the default is a newline. end="" is how you build a line across several prints — and you then need a bare print() to close it, which the page demonstrates.

print versus f-strings

print calls str() on whatever you give it, which is fine for quick output. When the formatting matters — decimal places, alignment, thousands separators — build the string yourself with an f-string and print that. The two are complementary, not competing.

One detail worth noticing: printing a list shows its repr, so strings appear with quotes. ", ".join(items) is what you want when the output is for a person.

Check yourself

0 of 3

Answer without scrolling back up.

  1. The user types 42. What does `input()` return?

  2. What does `print(i, end=' ')` do?

  3. Why does `int(input())` need a try/except in real programs?

Cheat sheet

input() and Output

input() reads one line from the user and returns it as a string — always a string, whatever it looks like. print() sends values the other way, with two options worth knowing.

PYTHON · vizlearn.in/python/input_and_output.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.