Input and output operations
Screen output
Python also provides functions to send program output to “standard output”, usually the screen or terminal.
The print()
function displays the value passed as a parameter:
name = "Eric"
print(name) # displays "Eric"
We can print multiple values separated by commas:
print("Hello", name, "!") # displays "Hello Eric!"
We can also use literal values without assigning to variables:
print("2 + 3 =", 2 + 3) # displays "2 + 3 = 5"
Output formatting
Python provides various ways to format output:
f-Strings: Allow inserting variables into a string:
name = "Eric"
print(f"Hello {name}") # displays "Hello Eric"
%s: Inserts string text into a format string:
name = "Eric"
print("Hello %s" % name) # displays "Hello Eric"
%d: Inserts integer numbers:
value = 15
print("The value is %d" % value) # displays "The value is 15"
.format(): Inserts values into a format string:
name = "Eric"
print("Hello {}. Welcome".format(name))
# 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 string.
Keyboard input
Python provides built-in functions to read data entered by the user at runtime. This is known as “standard input”.
The input()
function allows reading a value entered by the user and assigning it to a variable. For example:
name = 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
variable.
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()
, etc:
age = int(input("Enter your age: "))
pi = 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 commas:
name, 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 loop:
names = [] # empty list
for x in range(3):
name = input("Enter a name: ")
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()
function:
file = 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 file:
file.write("Hello World!")
file.write("This text goes to the file")
We must close the file with file.close()
when finished:
file.close()
We can also use with
to open and automatically close:
with open("data.txt", "w") as file:
file.write("Hello World!")
# no need to close, it's automatic
Reading files
To read a file we use open()
in “r” mode and iterate over the file object:
with open("data.txt", "r") as file:
for line in file:
print(line) # prints each line of the file
This prints each line, including newlines.
We can read all lines to a list with readlines()
:
lines = file.readlines()
print(lines)
To read the full content to a string we use read()
:
text = file.read()
print(text)
We can also read a specific number of bytes or characters with read(n)
.
File handling operations
There are several built-in functions to handle files in Python:
open()
- Opens a file and returns a file objectclose()
- Closes the filewrite()
- Writes data to the fileread()
- Reads data from the filereadline()
- Reads a line from the filetruncate()
- Empties the fileseek()
- Changes the reading/writing positionrename()
- Renames the fileremove()
- Deletes the file
These allow us to perform advanced operations to read, write and maintain files.
Conclusion
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 functions.
References
- Downey, A. B. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tea Press.
- McKinney, W. (2018). Python for data analysis: Data wrangling with Pandas, NumPy, and IPython. O’Reilly Media.
- Matthes, E. (2015). Python crash course: A hands-on, project-based introduction to programming. No Starch Press.
- Lutz, M. (2013). Learning Python: Powerful Object-Oriented Programming. O’Reilly Media, Incorporated.
Cheers for making it this far! I hope this journey through the programming universe has been as fascinating for you as it was for me to write down.
We’re keen to hear your thoughts, so don’t be shy – drop your comments, suggestions, and those bright ideas you’re bound to have.
Also, to delve deeper than these lines, take a stroll through the practical examples we’ve cooked up for you. You’ll find all the code and projects in our GitHub repository learn-software-engineering/examples-programming.
Thanks for being part of this learning community. Keep coding and exploring new territories in this captivating world of software!
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.