print('hi'+'there')
hithere
print('hi '+'there')
hi there
print('Number \tSquare \tCube')
for i in range(1,11):
print(i,'\t',i**2,'\t',i**3)
Number Square Cube
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('hi\\there')#第一个斜线是转义字符
hi\there
#使用格式字符串
#%s插入字符串
#%i插入整数
#%f插入浮点数
age=13
print("I'm %i years old!"%age)
I'm 13 years old!
#数字格式化
'''
%.2f:这就告诉 Python 要采用浮点数格式,而且小数点后面要显示两位。(注 意,Python 非常聪明,它会正确地把数字四舍五入为两位小数,而不是直接去掉多
余的数位。)
'''
dec_number=12.3456
print('It is %.2f degrees today.'%dec_number)
It is 12.35 degrees today.
#整数:%d或者%i
#整数格式化,数字会被截断,不是四舍五入
#浮点数:%f或者%F
#如果希望总是显示正负号,%号后加+
number=-100
lnumber=100
print('%+.8f'%number)
-100.00000000
print('%+.8f'%lnumber)
+100.00000000
如果你在打印格式化字符串时想打印一个百分号,就需要输入两个百分号,
就像你曾经用两个反斜线来打印一个反斜线一样。我们说第一个百分号对第二个百
分号进行了转义,就像在本章前面的术语箱中提到的一样:
print('100%%')
100%%
print('%f%%'%number)
-100.000000%