Skip to content

Informatics Practices · Ch 3 — Brief Overview of Python

Input and Output

3.8

Input and Output

A program becomes genuinely useful the moment it can talk to its user — accept data while it runs and show results back. Python handles both directions of this conversation with two built-in functions: input() brings values into the program from an input device such as the keyboard, and print() sends output to the screen.

Taking values with input()

The input() function pauses the program and waits for the user to type something. Its syntax is:

variable = input([Prompt])

Here Prompt is a message we may want displayed on the screen before the user types — for example, "Enter your age: ". The prompt is optional; input() works without it, but a prompt makes it clear to the user what is expected.

The single most important behaviour to remember: input() takes exactly what is typed at the keyboard, converts it into a string, and assigns that string to the variable on the left-hand side of the assignment operator (=). It does not matter whether the user types alphabets, numbers or special characters — everything arrives as a string.

>>> fname = input("Enter your first name: ")
Enter your first name: Arnab
>>> age = input("Enter your age: ")
Enter your age: 19

The variable fname receives the string 'Arnab', which is what we would expect for a name. But notice age: it receives '19' — the string consisting of the characters 1 and 9 — not the number 19.

Watch out

After age = input("Enter your age: "), the variable age holds the string '19', not the integer 19. Any input, however numeric it looks, is stored as a string until you convert it.

Converting the input to a number

Since input arrives as a string, we often need to change its datatype to an appropriate numeric type before doing arithmetic with it. The int() function converts the accepted string into an integer. If the string entered is non-numeric (say the user types "nineteen"), the conversion cannot happen and an error is generated.

#function int() to convert string to integer
>>> age = int(input("Enter your age: "))
Enter your age: 19
>>> type(age)
<class 'int'>

Here input() first collects '19' as a string, then int() immediately converts it, so age ends up holding a proper integer — confirmed by type(age) reporting <class 'int'>.

Displaying results with print()

For output, Python uses the print() function, which writes data to the standard output device — the screen. Its syntax is:

print(value)

A key detail: print() first evaluates the expression given to it and only then displays the result. So it does not merely echo what you wrote; it computes it.

print("Hello")
print(10*2.5)

The first statement displays the text Hello. In the second, Python evaluates the expression 10 multiplied by 2.5 to get 25.0, and then displays 25.0 on the screen.

How Python works through an expression, step by step

It helps to see the way Python evaluates an expression before printing or assigning it — one operation at a time, respecting precedence and parentheses.

= (20 + 30) * 40   # Step 1: parentheses force + to be done before *
= 50 * 40          # Step 2
= 2000             # Step 3
``` …