Input/Output in Python

Input-Output (I/O) Operations

I/O stands for input and output operations. As the name describes, it refers to getting data and displaying data. Python uses streams of data to ingest and display data. You have already encountered the print() function, which prints data to a console or terminal. The print function does not write data to a file or database so if you close the console, you will lose the output. In this lesson, we will talk about using the input function to get data, read/writing files, printing with string formatting.

Getting input from a user can be done with the input function. You will need to assign a variable to the input function, such as sYourName = input(). The problem with input is that it does not give the user any onscreen instructions. You will need to print your instructions to the user with print commands. Below is an example of how to ask a user for their name and provide a greeting.


print("Please enter your name: ")
sName = input()
print("Hello "+sName)

The exciting part now is learning about string concatenation and inputs at the same time. You are familiar with the print command, but we have yet to show how to print multiple variables and strings. Python uses the + operator to concat variables. It knows the difference between a number and a string. 2+2 is still 4, but “First “+”Name” is First Name. You do need to include spaces where spaces are appropriate.

File streams are operations conducted to read and write files. Time for the deep end. File streams use a function called open() to connect a stream to a file. A file path, in quotes, needs to be provided to the open function. Paths can be absolute or relative. A relative path starts at the current directly while the absolute path is the full path to the file. These lessons will use the relative path so all files need to be in the same directory as each other.


with open('filename.txt','r') as myFile:
   for line in myFile:
print(line)

If there is a file named filename.txt with some data in it then that data would be printed to the screen. If you are not concerned with formatting then you can use the red() function below. The read function is common for files like JSON files, where spacing doesn’t matter.


with open('filename.txt','r') as myFile:
   data=myfile.read()
print(data)

Writing a file is like reading, only the opposite. While that seems like a typo, it’s not. To write a file, you use the same while loop provided above, but you change the ‘r’ to ‘w’.


with open('NewFile.txt', 'w') as outfile:
   outfile.write("New file with some data")

String formatting is the use of the format function within python 3.1+. We discussed previously how you could contact two or more strings together with the + operator. You can provide more readable code by using {} operators in your print function when you include the .format() member function. We’ve included a few examples below:


print("{0}, {1}, {2}".format('a', 'b', 'c'))
print("{}, {}, {}".format('a', 'b', 'c'))
print("{0}{1}{0}".format('abra', 'cad'))

You may notice that line 1 has a numerical value in between the curly braces. Just like a list, there are positions, with 0 being the first element associated with each element in the format function. Also, take note that the format function is a member of the string in quotes and is enclosed in the print statement. It is not a member of the print statement.

Leave a Comment