AlgoMaster Logo

Input & Output

Last Updated: January 3, 2026

5 min read

Whether you're building a simple script or a complex web application, knowing how to read from and write to the console or files makes your programs far more functional.

In this chapter, we’ll explore various aspects of I/O in Python, including reading input from users, displaying output, and working with files.

User Input with input()

One of the most common ways to get input from users in Python is through the built-in input() function. This function pauses your program, waits for the user to type something, and then returns that input as a string.

Here's a simple example:

In this example, when run, the program will display the prompt "Enter your name:". Once the user types their name and presses Enter, the program greets them.

Input with Data Types

The input you get from the input() function is always a string. If you need a different data type, such as an integer or float, you will need to convert it. Here’s how you can do that:

Edge Cases in Input

Be cautious with user input. If the user enters something that can't be converted—like a letter when you expect a number—your program will raise a ValueError. To handle such cases gracefully, you can use a try-except block:

Output with print()

Displaying output in Python is primarily done using the print() function. This function can take multiple arguments and can format the output in various ways, which is immensely helpful.

Basic Output

The simplest use of print() looks like this:

You can also print multiple items, separated by commas:

String Formatting

To make your output more readable and maintainable, Python offers several ways to format strings. Here are three common methods:

f-Strings (Python 3.6+)

F-strings are a convenient way to embed expressions inside string literals:

str.format()

The str.format() method allows you to format strings using placeholders:

Percent Formatting

Though less common now, the old-style percent formatting is still supported:

Advanced Output Formatting

You can also format numbers, such as controlling decimal places:

Working with Files

Reading from and writing to files are essential skills for any developer. Python makes this straightforward through built-in functions.

Opening and Closing Files

You can open a file using the open() function. It requires the file name and the mode (e.g., read 'r', write 'w', append 'a'):

Using the with statement is beneficial because it automatically closes the file for you when the block is exited, even if an error occurs.

Reading from Files

To read the content of a file, you can use the read(), readline(), or readlines() methods:

Reading Line by Line

If the file is large, you might prefer to read it line by line:

Writing to Files

You can also append data to existing files by opening them in append mode ('a'):

Edge Cases with File Operations

Always handle potential errors, such as trying to read a non-existent file. You can do this using a try-except block:

Binary Input and Output

Sometimes, you’ll need to work with binary data, such as images or executable files. Python allows you to read and write binary files using the same open() function but with 'b' added to the mode.

Writing Binary Files

Here’s how you can write binary data:

Reading Binary Files

You can read a binary file in a similar way:

Working with binary files is essential in fields such as data science, game development, and web applications, where file formats often involve binary encoding.

Summary

To summarize, in this chapter, we explored:

  • How to gather user input through the input() function.
  • Techniques for displaying output using the print() function, including various formatting methods.
  • Reading from and writing to text and binary files, along with error handling for file operations.

In the next chapter, we will look at how comments can be used effectively in your code to enhance readability and maintainability, ensuring that your programs are not only functional but also easy to understand.