Prints formatted string

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

If you want to print a percent sign when you print format string, you need to enter two percent,
just as you have to use two backslash backslash as a print. We say that first percent sign percentage of the second
semicolon been escaped, just as mentioned earlier in this chapter, the term box:

print('100%%')
100%%
print('%f%%'%number)
-100.000000%

Published 145 original articles · won praise 6 · views 8045

Guess you like

Origin blog.csdn.net/sinat_23971513/article/details/105085022