python 数字转换为字符串(%)

  • 全局函数str
>>> str(123)
'123'
>>> str(1.23)
'1.23'
  • %d
>>> 'a month has %d days'%30
'a month has 30 days'
>>> 'a big month has %d days,a small month has %d days'%(31,30)
'a big month has 31 days,a small month has 30 days'
>>> 'a big month has %10d days'%30
'a big month has         30 days'

注:用%后面的替换前面的%d
注:%d中间的10说明长度为10,has后面已经有一个空格了。

  • %f
>>> 'the number is %.1f'%1.23
'the number is 1.2'
>>> 'the number is %10.1f'%1.23
'the number is        1.2'

注意:is后面已经有一个空格了。

你可能感兴趣的:(python 数字转换为字符串(%))