>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1/7)
'0.14285714285714285'
>>> 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...
>>> # The repr() of a string adds string quotes and backslashes:
... hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, world\n'
>>> # The argument to repr() may be any Python object:
... repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
有两种方式可以写平方和立方表:
>>> for x in range(1, 11):
... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
... # Note use of 'end' 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 100 1000
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"
1
2
大括号和其中的字符会被替换成传入 str.format() 的参数。大括号中的数值指明使用传入 str.format() 方法的对象中的哪一个:
>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam
1
2
3
4
如果在 str.format() 调用时使用关键字参数,可以通过参数名来引用值:
>>> print('This {food} is {adjective}.'.format(
... food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.
1
2
3
位置参数和关键字参数可以随意组合:
>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
other='Georg'))
The story of Bill, Manfred, and Georg.
1
2
3
‘!a’ (应用 ascii()),’!s’ (应用 str() )和 ‘!r’ (应用 repr() )可以在格式化之前转换值:
>>> import math
>>> print('The value of PI is approximately {}.'.format(math.pi))
The value of PI is approximately 3.14159265359.
>>> print('The value of PI is approximately {!r}.'.format(math.pi))
The value of PI is approximately 3.141592653589793.
1
2
3
4
5
字段名后允许可选的 ‘:’ 和格式指令。这允许对值的格式化加以更深入的控制。下例将 Pi 转为三位精度。
>>> import math
>>> print('The value of PI is approximately {0:.3f}.'.format(math.pi))
The value of PI is approximately 3.142.
1
2
3
在字段后的 ‘:’ 后面加一个整数会限定该字段的最小宽度,这在美化表格时很有用:
>>> import math
>>> print('The value of PI is approximately %5.3f.' % math.pi)
The value of PI is approximately 3.142.
1
2
3
更多的信息可以参见 printf-style String Formatting 一节。
f.read()
‘This is the entire file.\n’
f.read()
”
f.readline() 从文件中读取单独一行,字符串结尾会自动加上一个换行符( \n ),只有当文件最后一行没有以换行符结尾时,这一操作才会被忽略。这样返回值就不会有混淆,如果 f.readline() 返回一个空字符串,那就表示到达了文件末尾,如果是一个空行,就会描述为 ‘\n’,一个只包含换行符的字符串:
>>> f.readline()
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
>>> f.readline()
''
1
2
3
4
5
6
你可以循环遍历文件对象来读取文件中的每一行。这是一种内存高效、快速,并且代码简介的方式:
>>> for line in f:
... print(line, end='')
...
This is the first line of the file.
Second line of the file
1
2
3
4
5
如果你想把文件中的所有行读到一个列表中,你也可以使用 list(f) 或者 f.readlines()。
f.write(string) 方法将 string 的内容写入文件,并返回写入字符的长度:
>>> f.write('This is a test\n')
15
1
2
想要写入其他非字符串内容,首先要将它转换为字符串:
>>> f = open('workfile', 'rb+')
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5) # Go to the 6th byte in the file
5
>>> f.read(1)
b'5'
>>> f.seek(-3, 2) # Go to the 3rd byte before the end
13
>>> f.read(1)
b'd'
1
2
3
4
5
6
7
8
9
10
11
在文本文件中(没有以 b 模式打开),只允许从文件头开始寻找(有个例外是用 seek(0, 2) 寻找文件的最末尾处)而且合法的 偏移 值只能是 f.tell() 返回的值或者是零。其它任何 偏移 值都会产生未定义的行为。