首页 > 编程语言 > 详细

Python - 格式化(format())输出

时间:2017-09-09 13:34:46      阅读:290      评论:0      收藏:0      [点我收藏+]
  1. age = 25  
  2. name = ‘Caroline‘  
  3.   
  4. print(‘{0} is {1} years old. ‘.format(name, age)) #输出参数  
  5. print(‘{0} is a girl. ‘.format(name))  
  6. print(‘{0:.3} is a decimal. ‘.format(1/3)) #小数点后三位  
  7. print(‘{0:_^11} is a 11 length. ‘.format(name)) #使用_补齐空位  
  8. print(‘{first} is as {second}. ‘.format(first=name, second=‘Wendy‘)) #别名替换  
  9. print(‘My name is {0.name}‘.format(open(‘out.txt‘, ‘w‘))) #调用方法  
  10. print(‘My name is {0:8}.‘.format(‘Fred‘)) #指定宽度  

 



输出:

  1. Caroline is 25 years old.   
  2. Caroline is a girl.   
  3. 0.333 is a decimal.   
  4. _Caroline__ is a 11 length.   
  5. Caroline is as Wendy.   
  6. My name is out.txt  
  7. My name is Fred    .  

Python - 格式化(format())输出

原文:http://www.cnblogs.com/chuangshaogreat/p/7498016.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!