python print也支持参数格式化,与C言的printf似, 示例:
>>> str = "the length of (%s) is %d" %('Hello World',len('Hello World'))
>>> print(str)
the length of (Hello World) is 11
或者直接写道print里:
>>> print( "the length of (%s) is %d" %('Hello World',len('Hello World')))
the length of (Hello World) is 11
>>>
3.4 格式化输出16制整数
nHex = 0x20
#%x --- hex 十六进制
#%d --- dec 十进制
#%o --- oct 八进制
nAge = int(input("input your age plz:/n"))
if nAge > 0 and nAge < 120:
print('thanks!')
else:
print('bad age')
print( 'your age is %d/n' % nAge)
4.3. 输入浮点型
fWeight = 0.0
fWeight = float(input("input your weight: /n"))
print('your weight is %f' % fWeight)
4.4. 输入16进制数据
nHex = int(input('input hex value(like 0x20):/n'),16)
print( 'nHex = %x,nOct = %d/n' %(nHex,nHex))
4.5. 输入8进制数据
nOct = int(input('input oct value(like 020):/n'),8)
print ('nOct = %o,nDec = %d/n' % (nOct,nOct))
示例:
>>> nHex = 0x20
>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
nHex = 20,nDec = 32,nOct = 40