Last Updated: January 3, 2026
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.
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.
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:
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:
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.
The simplest use of print() looks like this:
You can also print multiple items, separated by commas:
To make your output more readable and maintainable, Python offers several ways to format strings. Here are three common methods:
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:
Though less common now, the old-style percent formatting is still supported:
You can also format numbers, such as controlling decimal places:
Reading from and writing to files are essential skills for any developer. Python makes this straightforward through built-in functions.
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.
To read the content of a file, you can use the read(), readline(), or readlines() methods:
If the file is large, you might prefer to read it line by line:
You can also append data to existing files by opening them in append mode ('a'):
Always handle potential errors, such as trying to read a non-existent file. You can do this using a try-except block:
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.
Here’s how you can write binary data:
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.
To summarize, in this chapter, we explored:
input() function.print() function, including various formatting methods.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.