python中的格式化输出

本文详细介绍了Python中使用print函数进行字符串格式化输出的方法,包括整数、16进制、浮点数和16进制数据的输出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 格式化输出整数

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值