首页 > 编程语言 > 详细

Python中print格式化输出

时间:2018-01-11 14:24:04      阅读:214      评论:0      收藏:0      [点我收藏+]
截取字符串输出,下面例子将只输出字符串的前3个字母      
>>> str="abcdefg"  
>>> print "%.3s" % str   
  abc   
按固定宽度输出,不足使用空格补全,下面例子输出宽度为10      
>>> str="abcdefg"  
>>> print "%10s" % str   
     abcdefg   
截取字符串,按照固定宽度输出      
>>> str="abcdefg"  
>>> print "%10.3s" % str   
         abc   

测试用例:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

str = "abcdefg"
print("%.3s"%str)

# dec = 123.567
# print("%.2d"%dec)
print("%8s"%str)

print("%8.2s"%str)

 

Python中print格式化输出

原文:https://www.cnblogs.com/wuheng1991/p/8267637.html

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