python format 格式化输出方法
作者:bitcarmanlee 时间:2023-12-24 16:26:19
0、前言
在python2.7及以上的版本,str.format()的方式为格式化提供了非常大的便利。与之前的%型格式化字符串相比,他显得更为方便与优越。下面我们就来看看format的具体用法。
1、常见的用法
二话不说,首先上代码,看看format的一些常用方法。
print "{:.2f}".format(3.1415926) #3.14,保留小数点后两位
print "{:+.2f}".format(3.1415926) #+3.14 带符号保留小数点后两位
print "{:+.2f}".format(-10) #-10.00 带符号保留小数点后两位
print "{:+.0f}".format(-10.00) #-10 不带小数
print "{:0>2d}".format(1) #01 数字补零 (填充左边, 宽度为2)
print "{:x<2d}".format(1) #1x 数字补x (填充右边, 宽度为4)
print "{:x<4d}".format(10) #10xx 数字补x (填充右边, 宽度为4)
print "{:,}".format(1000000) #1,000,000 以逗号分隔的数字格式
print "{:.2%}".format(0.12) #12.00% 百分比格式
print "{:.2e}".format(1000000) #1.00e+06 指数记法
print "{:<10d}".format(10) #10 左对齐 (宽度为10)
print "{:>10d}".format(10) # 10 右对齐 (默认, 宽度为10)
print "{:^10d}".format(10) # 10 中间对齐 (宽度为10)
1、格式符
‘f'表示浮点数
‘d'表示十进制整数. 将数字以10为基数进行输出
‘%'表示百分数. 将数值乘以100然后以fixed-point(‘f')格式打印, 值后面会有一个百分号
‘e'表示幂符号. 用科学计数法打印数字, 用'e'表示幂.
2、对齐与填充
^、<、>分别是居中、左对齐、右对齐,后面带宽度 :后面带填充字符,只能是一个字符,不指定的话默认就是空格。
2、format基础字符串替换
format中的字符串参数可以使用{num}来表示。0表示第一个,1表示第二个,以此类推。
为了更好了解上面的用法,首先我们来看看format的源码
def format(self, *args, **kwargs): # known special case of str.format
"""
S.format(*args, **kwargs) -> string
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
"""
pass
给大家翻译一把:
使用args和kwargs的替换返回S的格式化版本,替换由大括号('{‘和'}')标识。
再来看看实际的例子:
print "{0} and {1} is good for big data".format("python","java")
print "{} and {} is good for big data".format("python","java")
print "{1} and {0} and {0} is good for big data".format("python","java")
让代码run起来以后的结果:
python and java is good for big data
python and java is good for big data
java and python and python is good for big data
还可以为参数制定名字:
print "{language1} is as well as {language2}".format(language1="python",language2="java")
效果如下:
python is as well as java
3、通过集合下标的方式访问
下面的例子也可以达到目的
languages = ["python","java"]
print "{0[0]} is as well as {0[1]}".format(languages)
最后的效果:
python is as well as java
4、通过对象属性
format还经常使用在对象属性中。请看下面的例子:
class Person(object):
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return "name is: {current.name}, age is: {current.age}".format(current=self)
p = Person("leilei",18)
print p
最后的效果:
name is: leilei, age is: 18
来源:https://blog.csdn.net/bitcarmanlee/article/details/67647282
标签:python,format,格式化,输出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MSSQL木马修复,中木马后的处理方法
2024-01-21 10:47:13
在Python的Django框架中调用方法和处理无效变量
2023-06-12 06:22:19
详解如何创建并发布一个 vue 组件
2023-07-02 16:49:08
![](https://img.aspxhome.com/file/2023/0/139790_0s.gif)
jQuery 选择器的使用
2008-01-21 13:07:00
一个用Ajax做的用户名验证程序
2007-10-21 20:40:00
Python中使用socket发送HTTP请求数据接收不完整问题解决方法
2021-06-29 11:43:50
python中string模块各属性以及函数的用法介绍
2023-12-07 13:49:47
用python读写excel的方法
2021-11-11 10:10:50
微信小程序实现签字功能
2024-04-16 09:26:12
![](https://img.aspxhome.com/file/2023/1/136681_0s.gif)
switchery按钮的使用方法
2024-04-29 13:40:44
![](https://img.aspxhome.com/file/2023/3/132593_0s.png)
Python超简单容易上手的画图工具库(适合新手)
2021-12-06 04:05:23
![](https://img.aspxhome.com/file/2023/9/67809_0s.jpg)
asp+jsp+JavaScript动态实现添加数据行
2023-07-03 05:37:15
![](https://img.aspxhome.com/file/2023/9/114069_0s.png)
pandas中read_csv、rolling、expanding用法详解
2022-03-01 06:09:30
![](https://img.aspxhome.com/file/2023/7/118377_0s.jpg)
Python从零开始创建区块链
2021-02-12 12:21:14
![](https://img.aspxhome.com/file/2023/3/92203_0s.jpg)
教你如何开发Vite3插件构建Electron开发环境
2024-04-28 09:22:54
![](https://img.aspxhome.com/file/2023/2/133102_0s.png)
python使用__slots__让你的代码更加节省内存
2023-03-15 06:33:57
![](https://img.aspxhome.com/file/2023/5/77805_0s.jpg)
后端开发使用pycharm的技巧(推荐)
2021-11-16 14:50:07
![](https://img.aspxhome.com/file/2023/5/99155_0s.jpg)
pandas 小数位数 精度的处理方法
2022-12-16 20:40:05
Python pygame 项目实战事件监听
2023-05-31 21:33:20
![](https://img.aspxhome.com/file/2023/8/112908_0s.png)
Python 安装第三方库 pip install 安装慢安装不上的解决办法
2023-02-23 13:43:45
![](https://img.aspxhome.com/file/2023/7/65017_0s.png)