Python en:Input Output

Introduction
There will be situations where your program has to interact with the user. For example, you would want to take input from the user and then print some results back. We can achieve this using the input and print functions respectively.

For output, we can also use the various methods of the str (string) class. For example, you can use the rjust method to get a string which is right justified to a specified width. See help(str) for more details.

Another common type of input/output is dealing with files. The ability to create, read and write files is essential to many programs and we will explore this aspect in this chapter.

Input from user
Output:

$ python user_input.py   Enter text: sir No, it is not a palindrome $ python user_input.py   Enter text: madam Yes, it is a palindrome $ python user_input.py   Enter text: racecar Yes, it is a palindrome

How It Works:

We use the slicing feature to reverse the text. We've already seen how we can make slices from sequences using the seq[a:b] code starting from position a to position b. We can also provide a third argument that determines the step by which the slicing is done. The default step is 1</tt> because of which it returns a continuous part of the text. Giving a negative step, i.e., -1</tt> will return the text in reverse.

The input</tt> function takes a string as argument and displays it to the user. Then it waits for the user to type something and press the return key. Once the user has entered, the input</tt> function will then return that text.

We take that text and reverse it. If the original text and reversed text are equal, then the text is a palindrome.

Homework exercise:

Checking whether a text is a palindrome should also ignore punctuation, spaces and case. For example, "Rise to vote, sir." is also a palindrome but our current program doesn't say it is. Can you improve the above program to recognize this palindrome?

Files
You can open and use files for reading or writing by creating an object of the file</tt> class and using its read</tt>, readline</tt> or write</tt> methods appropriately to read from or write to the file. The ability to read or write to the file depends on the mode you have specified for the file opening. Then finally, when you are finished with the file, you call the close</tt> method to tell Python that we are done using the file.

Example:

Output:

$ python using_file.py   Programming is fun When the work is done if you wanna make your work also fun: use Python!

How It Works:

First, open a file by using the built-in open</tt> function and specifying the name of the file and the mode in which we want to open the file. The mode can be a read mode ('r'</tt>), write mode ('w'</tt>) or append mode ('a'</tt>). We can also by dealing with a text file ('t'</tt>) or a binary file ('b'</tt>). There are actually many more modes available and help(open)</tt> will give you more details about them. By default, <tt>open</tt> considers the file to be a 't'ext file and opens it in 'r'ead mode.

In our example, we first open the file in write text mode and use the <tt>write</tt> method of the file object to write to the file and then we finally <tt>close</tt> the file.

Next, we open the same file again for reading. We don't need to specify a mode because 'read text file' is the default mode. We read in each line of the file using the <tt>readline</tt> method in a loop. This method returns a complete line including the newline character at the end of the line. When an empty string is returned, it means that we have reached the end of the file and we 'break' out of the loop.

By deafult, the <tt>print</tt> function prints the text as well as an automatic newline to the screen. We are suppressing the newline by specifying <tt>end= '' </tt> because the line that is read from the file already ends with a newline character. Then, we finally <tt>close</tt> the file.

Now, check the contents of the <tt>poem.txt</tt> file to confirm that the program has indeed written and read from that file.

Pickle
Python provides a standard module called <tt>pickle</tt> using which you can store any Python object in a file and then get it back later. This is called storing the object persistently.

Example:

Output:

$ python pickling.py   ['apple', 'mango', 'carrot']

How It Works:

To store an object in a file, we have to first <tt>open</tt> the file in 'w'rite 'b'inary mode and then call the <tt>dump</tt> function of the <tt>pickle</tt> module. This process is called pickling.

Next, we retrieve the object using the <tt>load</tt> function of the <tt>pickle</tt> module which returns the object. This process is called unpickling.

Summary
We have discussed various types of input/output and also file handling and using the pickle module.

Next, we will explore the concept of exceptions.

Previous Next