Input and output operations

Input/output operations (I/O) allow a program to communicate and exchange data with the outside world. In this article we will see in detail input operations from the keyboard or a file, and output to the screen or a file.

Screen output

Python also provides functions to send program output to “standard output”, usually the screen or terminal1.

The print() function displays the value passed as a parameter:

1name = "Eric"
2print(name) # displays "Eric"

We can print multiple values separated by commas2:

1print("Hello", name, "!") # displays "Hello Eric!"

We can also use literal values without assigning to variables3:

1print("2 + 3 =", 2 + 3) # displays "2 + 3 = 5"

Output formatting

Python provides various ways to format output4:

f-Strings: Allow inserting variables into a string:

1name = "Eric"
2print(f"Hello {name}") # displays "Hello Eric"

%s: Inserts string text into a format string:

1name = "Eric"
2print("Hello %s" % name) # displays "Hello Eric"

%d: Inserts integer numbers:

1value = 15
2print("The value is %d" % value) # displays "The value is 15"

.format(): Inserts values into a format string:

1name = "Eric"
2print("Hello {}. Welcome".format(name))
3# displays "Hello Eric. Welcome"

These formatting options allow us to interpolate variables and values into text strings to generate custom outputs. We can combine multiple values and formats in a single output string2.

Keyboard input

Python provides built-in functions to read data entered by the user at runtime. This is known as “standard input”4.

The input() function allows reading a value entered by the user and assigning it to a variable. For example:

1name = input("Enter your name: ")

This displays the message “Enter your name: " and waits for the user to enter text and press Enter. That value is assigned to the name variable2.

The input() function always returns a string. If we want to ask for a number or other data type, we must convert it using int(), float(), etc1:

1age = int(input("Enter your age: "))
2pi = float(input("Enter the value of pi: "))

Reading multiple values

We can ask for and read multiple values on the same line separating them with commas3:

1name, age = input("Enter name and age: ").split()

The split() method divides the input into parts and returns a list of strings. We then assign the list elements to separate variables.

We can also read multiple lines of input with a loop4:

1names = [] # empty list
3for x in range(3):
4   name = input("Enter a name: ")
5   names.append(name)

This code reads 3 names entered by the user and adds them to a list.

Output to a file

In addition to printing to the screen, we can write output to a file using the open() function1:

1file = open("data.txt", "w")

This opens data.txt for writing (“w”) and returns a file object.

Then we use file.write() to write to that file3:

1file.write("Hello World!")
2file.write("This text goes to the file")

We must close the file with file.close() when finished4:


We can also use with to open and automatically close:

1with open("data.txt", "w") as file:
2   file.write("Hello World!")
3   # no need to close, it's automatic

Reading files

To read a file we use open() in “r” mode and iterate over the file object1:

1with open("data.txt", "r") as file:
2   for line in file:
3      print(line) # prints each line of the file

This prints each line, including newlines.

We can read all lines to a list with readlines()3:

1lines = file.readlines()

To read the full content to a string we use read()4:

1text =

We can also read a specific number of bytes or characters with read(n)2.

File handling operations

There are several built-in functions to handle files in Python1:

  • open() - Opens a file and returns a file object
  • close() - Closes the file
  • write() - Writes data to the file
  • read() - Reads data from the file
  • readline() - Reads a line from the file
  • truncate() - Empties the file
  • seek() - Changes the reading/writing position
  • rename() - Renames the file
  • remove() - Deletes the file

These allow us to perform advanced operations to read, write and maintain files.


In this article we explained Python input and output operations in detail, including reading from standard input and writing to standard output or files. Properly handling input and output is essential for many Python applications. I recommend practising with your own examples to master these functions3.


  1. McKinney, W. (2018). Python for data analysis: Data wrangling with Pandas, NumPy, and IPython. O’Reilly Media. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  2. Lutz, M. (2013). Learning Python: Powerful Object-Oriented Programming. O’Reilly Media, Incorporated. ↩︎ ↩︎ ↩︎ ↩︎

  3. Matthes, E. (2015). Python crash course: A hands-on, project-based introduction to programming. No Starch Press. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  4. Downey, A. B. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tea Press. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎