在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能。本章节我们将具体介绍 Python 的输入输出。
输出格式美化
Python输出值的方式: print() 函数。
可以使用 str.format() 函数来格式化输出值。
如果你希望将输出的值转成字符串,可以使用 repr() 或 str() 函数来实现。
str(): 函数返回一个用户易读的表达形式。
repr(): 产生一个解释器易读的表达形式。
例如
>>> s = 'Hello, Py40'
>>> str(s)
'Hello, Py40'
>>> repr(s)
"'Hello, Py40'"
>>> str(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'x 的值为: ' + repr(x) + ', y 的值为:' + repr(y) + '...'
>>> print(s)
x 的值为: 32.5, y 的值为:40000...
>>> # repr() 函数可以转义字符串中的特殊字符
... hello = 'hello, Py40\n'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, Py40\n'
>>> # repr() 的参数可以是 Python 的任何对象
... repr((x, y, ('Google', 'Py40')))
"(32.5, 40000, ('Google', 'Py40'))"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>>s='Hello, Py40'
>>>str(s)
'Hello, Py40'
>>>repr(s)
"'Hello, Py40'"
>>>str(1/7)
'0.14285714285714285'
>>>x=10*3.25
>>>y=200*200
>>>s='x 的值为: '+repr(x)+', y 的值为:'+repr(y)+'...'
>>>print(s)
x的值为:32.5,y的值为:40000...
>>># repr() 函数可以转义字符串中的特殊字符
...hello='hello, Py40\n'
>>>hellos=repr(hello)
>>>print(hellos)
'hello, Py40\n'
>>># repr() 的参数可以是 Python 的任何对象
...repr((x,y,('Google','Py40')))
"(32.5, 40000, ('Google', 'Py40'))"
这里有两种方式输出一个平方与立方的表:
>>> for x in range(1, 11):
... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
... # 注意前一行 'end' 的使用
... 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
>>> for x in range(1, 11):
... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
>>>forxinrange(1,11):
...print(repr(x).rjust(2),repr(x*x).rjust(3),end=' ')
...# 注意前一行 'end' 的使用
...print(repr(x*x*x).rjust(4))
...
111
248
3927
41664
525125
636216
749343
864512
981729
101001000
>>>forxinrange(1,11):
...print('{0:2d} {1:3d} {2:4d}'.format(x,x*x,x*x*x))
...
111
248
3927
41664
525125
636216
749343
864512
981729
101001000
注意:在第一个例子中, 每列间的空格由 print() 添加。
这个例子展示了字符串对象的 rjust() 方法, 它可以将字符串靠右, 并在左边填充空格。
还有类似的方法, 如 ljust() 和 center()。 这些方法并不会写任何东西, 它们仅仅返回新的字符串。
另一个方法 zfill(), 它会在数字的左边填充 0,如下所示:
>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'
1
2
3
4
5
6
>>>'12'.zfill(5)
'00012'
>>>'-3.14'.zfill(7)
'-003.14'
>>>'3.14159265359'.zfill(5)
'3.14159265359'
str.format() 的基本使用如下:
>>> print('{}网址: "{}!"'.format('python在线学习', 'www.py40.com'))
python在线学习网址: "www.py40.com!"
1
2
>>>print('{}网址: "{}!"'.format('python在线学习','www.py40.com'))
python在线学习网址:"www.py40.com!"
括号及其里面的字符 (称作格式化字段) 将会被 format() 中的参数替换。
在括号中的数字用于指向传入对象在 format() 中的位置,如下所示:
>>> print('{0} 和 {1}'.format('Google', 'py40'))
Google 和 py40
>>> print('{1} 和 {0}'.format('Google', 'py40'))
py40 和 Google
1
2
3
4
>>>print('{0} 和 {1}'.format('Google','py40'))
Google和py40
>>>print('{1} 和 {0}'.format('Google','py40'))
py40和Google
如果在 format() 中使用了关键字参数, 那么它们的值会指向使用该名字的参数。
>>> print('{name}网址: {site}'.format(name='python在线学习', site='www.py40.com'))
1
>>>print('{name}网址: {site}'.format(name='python在线学习',site='www.py40.com'))
位置及关键字参数可以任意的结合:
>>> print('站点列表 {0}, {1}, 和 {other}。'.format('Google', 'py40.com',
other='Taobao'))
站点列表 Google, py40.com, 和 Taobao。
1
2
3
>>>print('站点列表 {0}, {1}, 和 {other}。'.format('Google','py40.com',
other='Taobao'))
站点列表Google,py40.com,和Taobao。
‘!a’ (使用 ascii()), ‘!s’ (使用 str()) 和 ‘!r’ (使用 repr()) 可以用于在格式化某个值之前对其进行转化:
>>> import math
>>> print('常量 PI 的值近似为: {}。'.format(math.pi))
常量 PI 的值近似为: 3.141592653589793。
>>> print('常量 PI 的值近似为: {!r}。'.format(math.pi))
常量 PI 的值近似为: 3.141592653589793。
1
2
3
4
5
>>>importmath
>>>print('常量 PI 的值近似为: {}。'.format(math.pi))
常量PI的值近似为:3.141592653589793。
>>>print('常量 PI 的值近似为: {!r}。'.format(math.pi))
常量PI的值近似为:3.141592653589793。
可选项 ‘:’ 和格式标识符可以跟着字段名。 这就允许对值进行更好的格式化。 下面的例子将 Pi 保留到小数点后三位:
>>> import math
>>> print('常量 PI 的值近似为 {0:.3f}。'.format(math.pi))
常量 PI 的值近似为 3.142。
1
2
3
>>>importmath
>>>print('常量 PI 的值近似为 {0:.3f}。'.format(math.pi))
常量PI的值近似为3.142。
在 ‘:’ 后传入一个整数, 可以保证该域至少有这么多的宽度。 用于美化表格时很有用。
>>> table = {'Google': 1, 'py40com': 2, 'Taobao': 3}
>>> for name, number in table.items():
... print('{0:10} ==> {1:10d}'.format(name, number))
...
py40com ==> 2
Taobao ==> 3
Google ==> 1
1
2
3
4
5
6
7
>>>table={'Google':1,'py40com':2,'Taobao':3}
>>>forname,numberintable.items():
...print('{0:10} ==> {1:10d}'.format(name,number))
...
py40com==>2
Taobao==>3
Google==>1
如果你有一个很长的格式化字符串, 而你不想将它们分开, 那么在格式化时通过变量名而非位置会是很好的事情。
最简单的就是传入一个字典, 然后使用方括号 ‘[]’ 来访问键值 :
>>> table = {'Google': 1, 'py40com': 2, 'Taobao': 3}
>>> print('Py40: {0[py40com]:d}; Google: {0[Google]:d}; '
'Taobao: {0[Taobao]:d}'.format(table))
py40com: 2; Google: 1; Taobao: 3
1
2
3
4
>>>table={'Google':1,'py40com':2,'Taobao':3}
>>>print('Py40: {0[py40com]:d}; Google: {0[Google]:d}; '
'Taobao: {0[Taobao]:d}'.format(table))
py40com:2;Google:1;Taobao:3
也可以通过在 table 变量前使用 ‘**’ 来实现相同的功能:
>>> table = {'Google': 1, 'py40com': 2, 'Taobao': 3}
>>> print('Py40: {py40com:d}; Google: {Google:d}; Taobao: {Taobao:d}'.format
(**table))
py40.com: 2; Google: 1; Taobao: 3
1
2
3
4
>>>table={'Google':1,'py40com':2,'Taobao':3}
>>>print('Py40: {py40com:d}; Google: {Google:d}; Taobao: {Taobao:d}'.format
(**table))
py40.com:2;Google:1;Taobao:3
旧式字符串格式化
% 操作符也可以实现字符串格式化。 它将左边的参数作为类似 sprintf() 式的格式化字符串, 而将右边的代入, 然后返回格式化后的字符串. 例如:
>>> import math
>>> print('常量 PI 的值近似为:%5.3f。' % math.pi)
常量 PI 的值近似为:3.142。
1
2
3
>>>importmath
>>>print('常量 PI 的值近似为:%5.3f。'%math.pi)
常量PI的值近似为:3.142。
因为 str.format() 比较新的函数, 大多数的 Python 代码仍然使用 % 操作符。但是因为这种旧式的格式化最终会从该语言中移除, 应该更多的使用 str.format().
读取键盘输入
Python提供了 input() 置函数从标准输入读入一行文本,默认的标准输入是键盘。
input 可以接收一个Python表达式作为输入,并将运算结果返回。
str = input("请输入:");
print ("你输入的内容是: ", str)
1
2
str=input("请输入:");
print("你输入的内容是: ",str)
结果:
请输入:py40.com
你输入的内容是: py40.com
1
2
请输入:py40.com
你输入的内容是:py40.com