Pythons Input and Output Kanel Nang Output Formatting














- Slides: 14

Python’s Input and Output Kanel Nang

Output Formatting � Two methods of formatting output ◦ Standard string slicing and concatenation operations ◦ str. format() method ie. >>> a = “The sum of 1 + 2 is {0}”. format(1+2) >>> print a The sum of 1 + 2 is 3

Output Formatting cont. � How do you convert value to string in Python? ◦ Functions �Repr() �Str() ◦ Differences �Str() function is meant to return representation of values that is human-readable �Repr() generate representations can be read by interpreter �If str() does have a proper human representation, then it will return the same value as repr()

Output Format Examples � >>> s = ‘hello, worldn’ � >>> hello = repr(s) � >>> print hello � ‘hello, worldn’ � >>> hellos = str(s) � >>> print hellos � hello, world � >>> x = 10 * 3. 25 � >>> y = 200 * 200 � >>> s = ‘The value of x is ‘ + repr(x) + ‘, and y is ‘ + repr(y) + ‘…’ � >>> print s � The value of x is 32. 5, and y is 40000…

Output Formatting cont. � Useful ◦ rjust() methods of string objects �Right-justifies a string in a field of a given width by padding spaces on the left ◦ ljust() �Left-justifies a string in a field ◦ center() �Center-justifies a string in a field ◦ zfill() Pads a numeric string on the left with zeros

Output Format Examples cont. >>> for x in range(1, 11): �. . . print repr(x). rjust(2), repr(x*x). rjust(3), �. . . # Note trailing comma on previous line �. . . print repr(x*x*x). rjust(4) � 1 1 1 � 2 4 8 � 3 9 27 � 4 16 64 � 5 25 125 � 6 36 216 � 7 49 343 � 8 64 512 � 9 81 729 � 10 1000 �

Output Format Examples cont. � >>> ‘ 12’. zfill(5) � ‘ 00012’ � >>> ‘-3. 14’. zfill(7) � ‘-003. 14’ � >>> ’ 3. 14159265359’. zfill(5) � ‘ 3. 1415926359’

Output Formatting cont. � Old string formatting ◦ The % operator is another method for string formatting. ◦ Similar to sprintf() in that it interprets the left argument to be applied to the right argument, then return the result of the string. �ie. >>> import math >>> print ‘The value of PI is approximately %5. 3 f. ’ % math. pi The value of PI is approximately 3. 142

Reading and Writing Files � Open() ◦ Returns a file object and is most commonly used with two arguments �ie. �>>> open(filename, mode) Modes ◦ ◦ ‘r’ – read only ‘w’ - writing only (overwrite existing files with same name) ‘a’ – opens file for appending (new data added to the end) ‘r+’ - opens the file in both reading and writing ◦ The mode argument is optional, ‘r’ will be assumed if it’s omitted

Reading and Writing Files cont. � Other modes ◦ On Windows �‘b’ appended to the mode opens file in binary mode �ie. �‘rb’, ‘wb’, and ‘r+b’ ◦ Python on windows will make a distinction between text and binary files, therefore the end-of-line characters in text files will be automatically altered slighted when data is read or written �Safe for ASCII text files, currupt binary data such as JPEG or EXE files

Methods of File Objects � Methods ◦. read(size) - reads some quantity of data and returns it as a string, if size is omitted, the entire content will be read and returned ◦. readline() - reads a single line from a file (n should be left at the end of a string and should only be omitted on the last line ◦. readlines() - returns a list containing all the lines of a data file (size bytes can be included to avoid overloading memory) ◦. write() – writes the content of a string to the file ◦. tell() – returns an integer giving the file object’s current position in the file measured in bytes ◦. seek() - change the files object’s position ◦. close() – closes the file

Methods of File Objects examples � � � � >>> F. read() ‘This is the entre file. n’ >>> F. readline() ‘This is the firstline of the file. n’ >>> F. readline() ‘Second line of the filen’ >>> F. readlines() [‘This is the first line of the file. n’, ‘Second line of the filen’] >>> >>> ‘ 5’ F = open(‘/tmp/workfile’, ‘r+’) F. write(‘ 0123456789 abcdef’) F. seek(5) #Go to the 6 th byte in the file F. read(1)

The pickle Module � pickling ◦ A module that can take almost any Python object and convert it to a string representation. �ie. �You have object x and a file object f that’s open for writing �Pickle. dump(x, f) unpickling ◦ Reconstructing the object from the string representation ◦ ie. ◦ f is a file object opened for reading ◦ X = pickle. load(f) The standard way to make Python objects which can be stored and reused by other programs

Resource � http: //docs. python. org/tutorial/inputoutput. html