Basic Input and Output in Python

This text is part of a Real Python tutorial by Martin Breuss.


For a program to be useful, it often needs to communicate with the outside world. In Python, the input() function allows you to capture user input from the keyboard, while you can use the print() function to display output to the console.

These built-in functions allow for basic user interaction in Python scripts, enabling you to gather data and provide feedback. If you want to go beyond the basics, then you can even use them to develop applications that are not only functional but also user-friendly and responsive.

By the end of this lesson, you’ll know how to:

  • Take user input from the keyboard with input()
  • Display output to the console with print()
  • Format output using the sep and end keyword arguments of print()

Reading Input From the Keyboard

Programs often need to obtain data from users, typically through keyboard input. In Python, one way to collect user input from the keyboard is by calling the input() function:

The input() function pauses program execution to allow you to type in a line of input from the keyboard. Once you press the Enter key, all characters typed are read and returned as a string, excluding the newline character generated by pressing Enter.

If you add text in between the parentheses, effectively passing a value to the optional prompt argument, then input() displays the text you entered as a prompt:

Language: Python
>>> name = input("Please enter your name: ")
Please enter your name: John Doe
>>> name
'John Doe'

Adding a meaningful prompt will assist your user in understanding what they’re supposed to input, which makes for a better user experience.

The input() function always reads the user’s input as a string. Even if you type characters that resemble numbers, Python will still treat them as a string:

Language: Python
 1>>> number = input("Enter a number: ")
 2Enter a number: 50
 3
 4>>> type(number)
 5<class 'str'>
 6
 7>>> number + 100
 8Traceback (most recent call last):
 9  File "<python-input-1>", line 1, in <module>
10    number + 100
11    ~~~~~~~^~~~~
12TypeError: can only concatenate str (not "int") to str

In the example above, you wanted to add 100 to the number entered by the user. However, the expression number + 100 on line 7 doesn’t work because number is a string ("50") and 100 is an integer. In Python, you can’t combine a string and an integer using the plus (+) operator.

You wanted to perform a mathematical operation using two integers, but because input() always returns a string, you need a way to read user input as a numeric type. So, you’ll need to convert the string to the appropriate type:

Language: Python
>>> number = int(input("Enter a number: "))
Enter a number: 50

>>> type(number)
<class 'int'>

>>> number + 100
150

In this updated code snippet, you use int() to convert the user input to an integer right after collecting it. Then, you assign the converted value to the name number. That way, the calculation number + 100 has two integers to add. The calculation succeeds and Python returns the correct sum.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

You must own this product to join the conversation.