str() return representations of values which are fairly human-readable,
repr() return representations which can be read by interpreter.
1234567891011121314
>>>s='Hello, world.'>>>str(s)'Hello, world.'>>>repr(s)"'Hello, world.'"# repr() of a string adds string quotes and backslashes>>>hello='hello, world\n'>>>hellos=repr(hello)>>>print(hellos)'Hello, world\n'>>>x=10*3.25>>>s='The value of x is'+'repr(x)>>>print(s)>>>repr((x,y,('spam','eggs')))# any object can be used"(32.5,40000,('spam','eggs'))"
Write A Table
str.rjust(padding size): right-justifies a string of a given width by padding it with spaces on the left. str.ljust() and str.center() are similar.
>>>print('We are the {} who say "{}!"'.format('knights','Ni'))Wearetheknightswhosay"Ni!"
The anything with brackets will be replaced by the objects passed into the str.format(). You can use number in the brackets to refer to the position.
123456
>>>print('{0} and {1}'.format('spam','eggs'))spamandeggs>>>print('{1} and {0}'.format('spam','eggs'))eggsandspam
If you set up keywords within brackets, their values can be referred by using the name of argument.
123
>>>print('This {food} is {adjective}.'.format(food='spam',adjective='absolutely horrible'))Thisspamisabsolutelyhorrible.
You can combin the number and keyword
123
>>>print('The story of {0}, {1}, and {other}.'.format('Bill','Manfred',other='Georg'))ThestoryofBill,Manfred,andGeorg.
!a (ascii()), !s (str()) and !r (repr()) can be used to convert the value before it is formateed
1234567
>>>importmath>>>print('The value of PI is approximately {}.'.format(math.pi))ThevalueofPIisapproximately3.141592653559>>>print('The value of PI is approximately {!r}.'.format(math.pi))ThevalueofPIisapproximately3.141592653589793
An optional : and format specifier can follow the field name.
1234
>>>importmath>>>print('The value of PI is approximately {0:.3f}.'.format(math.pi))ThevalueofPIisapproximately3.142# three places after the decimal.
Passing an integer after the ‘:’ will cause that field to be a minimum number of characters wide.
% can also be used for string formatting, like printf()
1234
>>>importmath>>>print('The value of PI is approximately %5.3f.'%math.pi)ThevalueofPIisapproximately3.142
Reading and Writing Files
open() is used with two arguments open(filename, mode) and return a file object
1
>>>f=open('workfile','w')
filename is a string that contains the filename.
mode ca be r when file is read only, w for only writing and a opens the file for appending. r+ opens the file for both reading and writing.
b open the file in binary mode. In normal files are opened in text mode
By default, when reading in text mode, line endings (\n or \r\n) will be read.
After you create the file object f. you can do the following things.
f.read() can read a file and return the content as a string. You can also specify how much to read by f.read(size). If the end of the file has been reached, f.read() will return an empty string (‘’)
1234
>>>f.read()`Thisistheentirefile.\n'>>>f.read()''
f.readline() can read a single line from the file. A newline character (\n) will be left at the end of the string. When the end of file is reached, an empty string will be returned. A blank line is represented by ‘\n’.
f.tell() returns an integer giving the file object’s current position in the file.
f.seek(offset, from_what) change the file object’s position.
By default, from_what is 0: from the begining of the file.
1: use the current position
** 2: use the end of the file.
1234567891011
>>>f=open('workfile','rb+')>>>f.write(b'0123456789abcdef')16>>>f.seek(5)# Go to the 6th byte in the file5>>>f.read(1)b'5'>>>f.seek(-3,2)# Go to the 3rd byte before the end13>>>f.read(1)b'd'
In text files, ONLY seeks relative to the beginning of the file. Offset values must be 0 or f.tell()
f.close can close the file you opened.
It is good to use with keyword when dealing with file objects.