1. Format string: str.Format()
2. Convert to string: str(), repr()
2.1 The str() function is meant to return representations of values which are fairly human-readable
2.2 repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is no equivalent syntax).
2.3 For objects which don’t have a particular representation for human consumption, str() will return the same value as repr().
2.4 The repr() of a string adds string quotes and backslashes
"'hello\\r'">>> s='hello\n'>>> str(s)'hello\n'>>> repr(s)"'hello\\n'"
3. str.rjust(), str.ljust(), str.center()
4. str.zfill()
5. str.format()
5.1 >>> print(’The story of {0}, {1}, and {other}.’.format(’Bill’, ’Manfred’, other=’Georg’))
5.2 >>> print(’We are the {} who say "{}!"’.format(’knights’, ’Ni’))
5.3 >>> print(’The value of PI is approximately {!r}.’.format(math.pi))
5.3.1 ’!a’ (apply ascii()),
5.3.2 ’!s’ (apply str())
5.3.3 ’!r’ (apply repr())
5.4 >>> table = {’Sjoerd’: 4127, ’Jack’: 4098, ’Dcab’: 8637678}
>>> print(’Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; Dcab: {0[Dcab]:d}’.format(table))
or
>>> print(’Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}’.format(**table))
5.5 (old style) print(’The value of PI is approximately %5.3f.’ % math.pi)
6. open() return file object
6.1 Reading in text mode (default UTF-8), platform-specific line endings will be converted to \n automatically. Similar translation happens on writing.
7. f.read(size)
7.1 if no size or size is negative, read the whole file
7.2 read to the end, return empty string ('')
8. f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline.
9. For reading lines from a file, you can loop over the file object.
>>> for line in f:... print(line, end=’’)...
10. f.write() returns how many bytes written into file.
11. f.seek(), f.tell()
12.
pickle
Module
12.1 pickling: take any Python object to a string
12.2 unpickling: reconstruct the object from a string
12.3 EASY TO USE, WIDELY SUPPORTED!!