input()
function and print
function respectively.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.io_input.py
: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
because of which it returns a continuous part of the text. Giving a negative step, i.e., -1
will return the text in reverse.input()
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 and pressed the return key, the input()
function will then return that text the user has entered.file
class and using its read
, readline
or write
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
method to tell Python that we are done using the file.io_using_file.py
):open
method. We open (or create it if it doesn't already exist) this file by using the built-in open
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'
), write mode ('w'
) or append mode ('a'
). We can also specify whether we are reading, writing, or appending in text mode ('t'
) or binary mode ('b'
). There are actually many more modes available and help(open)
will give you more details about them. By default, open()
considers the file to be a 't'ext file and opens it in 'r'ead mode.write
method of the file object to write our string variable poem
to the file and then we finally close
the file.readline
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.close
the file.readline
output that this program has indeed written to and read from our new poem.txt
file.pickle
which you can use to store any plain Python object in a file and then get it back later. This is called storing the object persistently.io_pickle.py
):open
the file in write binary mode and then call the dump
function of the pickle
module. This process is called pickling.load
function of the pickle
module which returns the object. This process is called unpickling.NOTE: If you are using Python 2, and we want to be able to read and write other non-English languages, we need to use theunicode
type, and it all starts with the characteru
, e.g.u"hello world"
open
function.io.open
and then use the encoding
argument in the first open statement to encode the message, and then again in the second open statement when decoding the message. Note that we should only use encoding in the open statement when in text mode.u
before the string) like we have used above, we have to make sure that Python itself is told that our program uses UTF-8, and we have to put # encoding=utf-8
comment at the top of our program.