Python学习笔记6:输入输出
print()
print默认是换行的,是end=”
“在起作用。
要想不换行你应该写成 print(str, end = “”)
>>> print("The length of %s is %d." %("Python", len("Python")))
The length of Python is 6.
看看《Python基础编程》中对格式化输出的总结:
(1). %字符:标记转换说明符的开始
(2). 转换标志:-表示左对齐;+表示在转换值之前要加上正负号;“”(空白字符)表示正数之前保留空格;0表示转换值若位数不够则用0填充
(3). 最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是,则宽度会从值元组中读出。
(4). 点(.)后跟精度值:如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就表示最大字段宽度。如果是,那么精度将从元组中读出
>>> pi = 3.141592653
>>> print("%10.3f" % pi) #字段宽10,精度3
3.142
>>> print("pi = %.*f" % (3,pi)) #用*从后面的元组中读取字段宽度或精度
pi = 3.142
>>> print("%010.3f" % pi) #用0填充空白
000003.142
>>> print("%-10.3f" % pi) #左对齐
3.142
>>> print("%+f" % pi) #显示正负号
+3.141593
print()与format()一起输出:
print("b - 二进制。将数字以2为基数进行输出: {0:b}".format(50))
print("c - 字符。在打印之前将整数转换成对应的Unicode字符串: {0:c}".format(50))
print("d - 十进制整数。将数字以10为基数进行输出: {0:d}".format(50))
print("o - 八进制。将数字以8为基数进行输出: {0:o}".format(50))
print("x - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母: {0:x}".format(50))
print("e - 幂符号。用科学计数法打印数字。用e表示幂: {0:e}".format(50))
print("g - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印: {0:g}".format(50))
print("n - 数字。当值为整数时和d相同,值为浮点数时和g相同。不同的是它会根据区域设置插入数字分隔符: {0:n}".format(50.12))
print("% - 百分数。将数值乘以100然后以fixed-point(f)格式打印,值后面会有一个百分号: {0:%}".format(50.12))
# 输出如下:
b - 二进制。将数字以2为基数进行输出: 110010
c - 字符。在打印之前将整数转换成对应的Unicode字符串: 2
d - 十进制整数。将数字以10为基数进行输出: 50
o - 八进制。将数字以8为基数进行输出: 62
x - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母: 32
e - 幂符号。用科学计数法打印数字。用e表示幂: 5.000000e+01
g - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印: 50
n - 数字。当值为整数时和d相同,值为浮点数时和g相同。不同的是它会根据区域设置插入数字分隔符: 50.12
% - 百分数。将数值乘以100然后以fixed-point(f)格式打印,值后面会有一个百分号: 5012.000000%
format()
>>> print("The name is {}, age is {}".format("James", 18))
The name is James, age is 18
>>> print("The name is {0}, age is {1}".format("James", 18))
The name is James, age is 18
>>> print("The name is {1}, age is {0}".format("James", 18))
The name is 18, age is James
>>> print("The name is {name}, age is {age}".format(name="James", age=18))
The name is James, age is 18
>>> print("The name is {0}, age is {1}, fav is {other}".format("James", 18, other="ball"))
The name is James, age is 18, fav is ball
>>>
>>> import math
>>> print("The value of PI is {}".format(math.pi))
The value of PI is 3.141592653589793
>>> print("The value of PI is {!a}".format(math.pi))
The value of PI is 3.141592653589793
>>> print("The value of PI is {!s}".format(math.pi))
The value of PI is 3.141592653589793
>>> print("The value of PI is {!r}".format(math.pi))
The value of PI is 3.141592653589793
>>> print("The value of PI is {0:.2f}".format(math.pi))
The value of PI is 3.14
>>>
>>> table = {"chinese":88, "math":96, "english":99}
>>> for x, y in table.items():
print("{0:10} => {1:10d}".format(x, y))
chinese => 88
math => 96
english => 99
>>>
>>> for x, y in table.items():
print("{0:>10} => {1:<10d}".format(x, y)) # >右对齐, <(默认)左对齐
chinese => 88
math => 96
english => 99
>>>
>>> for x, y in table.items():
print("{0:^10} => {1:=10d}".format(x, y)) # ^中间对齐
chinese => 88
math => 96
english => 99
>>> table = {"chinese":88, "math":96, "english":99}
>>> print("chinese:{0[chinese]:d}, math:{0[math]:d}, englist:{0[english]:d}".format(table))
chinese:88, math:96, englist:99
>>>