python format格式化函数详解

简介: python format格式化函数详解

format函数介绍

Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。


基本语法是通过 {} 和 : 来代替以前的 % 。

format 函数可以接受不限个参数,位置可以不按顺序。

字符串的参数使用{NUM}进行表示,0, 表示第一个参数,1, 表示第二个参数, 以后顺次递加;

使用":", 指定代表元素需要的操作, 如":.3"小数点三位, ":8"占8个字符空间等;

还可以添加特定的字母, 如:

'b' - 二进制. 将数字以2为基数进行输出.
'c' - 字符. 在打印之前将整数转换成对应的Unicode字符串.
'd' - 十进制整数. 将数字以10为基数进行输出.
'o' - 八进制. 将数字以8为基数进行输出. 
'x' - 十六进制. 将数字以16为基数进行输出, 9以上的位数用小写字母.
'e' - 幂符号. 用科学计数法打印数字, 用'e'表示幂. 
'g' - 一般格式. 将数值以fixed-point格式输出. 当数值特别大的时候, 用幂形式打印. 
'n' - 数字. 当值为整数时和'd'相同, 值为浮点数时和'g'相同. 不同的是它会根据区域设置插入数字分隔符. 
'%' - 百分数. 将数值乘以100然后以fixed-point('f')格式打印, 值后面会有一个百分号. 

format 格式转换

数字       格式    输出     描述
3.1415926 {:.2f}    3.14      保留小数点后两位
3.1415926 {:+.2f}   3.14      带符号保留小数点后两位  
-1          {:+.2f}   -1      带符号保留小数点后两位
2.71828     {:.0f}    3         不带小数
1000000     {:,}    1,000,000 以逗号分隔的数字格式
0.25      {:.2%}    25.00%  百分比格式
1000000000  {:.2e}    1.00E+09  指数记法
25          {0:b}   11001 转换成二进制
25          {0:d}   25  转换成十进制
25          {0:o}   31  转换成八进制
25          {0:x}   19  转换成十六进制
5         {:0>2}    05  数字补零(填充左边, 宽度为2)
5         {:x<4}    5xxx  数字补x (填充右边, 宽度为4)
10          {:x^4}    x10x  数字补x (填充两边,优先左边, 宽度为4)
13          {:10}   13  右对齐 (默认, 宽度为10)
13          {:<10}    13  左对齐 (宽度为10)
13          {:^10}    13  中间对齐 (宽度为10)

format实战

1.位置填充字符串

print('hello {0} i am {1}'.format('world','python'))    # 输入结果:hello world i am python
print('hello {} i am {}'.format('world','python') )     #输入结果:hello world i am python
print('hello {0} i am {1} . a now language--{1}'.format('world','python')# 输出结果:hello world i am python . a now language-- python
[root@localhost ~]# cat test.py
#!/usr/bin/python
#coding:utf-8
age = 25
name = 'Caroline'
print('{0} is {1} years old. '.format(name, age)) #输出参数
print('{0} is a girl. '.format(name))
print('{0:.3} is a decimal. '.format(1/3)) #小数点后三位
print('{0:_^11} is a 11 length. '.format(name)) #使用_补齐空位
print('{first} is as {second}. '.format(first=name, second='Wendy')) #别名替换
print('My name is {0.name}'.format(open('out.txt', 'w'))) #调用方法
print('My name is {0:8}.'.format('Fred')) #指定宽度
[root@localhost ~]# python3.8 test.py
Caroline is 25 years old. 
Caroline is a girl. 
0.333 is a decimal. 
_Caroline__ is a 11 length. 
Caroline is as Wendy. 
My name is out.txt
My name is Fred    .

foramt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 ……

也可以不输入数字,这样也会按顺序来填充

同一个参数可以填充多次,这个是format比%先进的地方

2.key来填充

obj = 'world'
name = 'python'
print('hello, {obj} ,i am {name}'.format(obj = obj,name = name)) # 输入结果:hello, world ,i am python

3.列表填充

list=['world','python']
print('hello {names[0]}  i am {names[1]}'.format(names=list))  # 输出结果:hello world  i am python
print('hello {0[0]}  i am {0[1]}'.format(list))   #输出结果:hello world  i am python

4.字典填充

dict={‘obj’:’world’,’name’:’python’}
print(‘hello {names[obj]} i am {names[name]}’.format(names=dict)) # hello world i am python

注意:访问字典的key,不用引号

5.类的属性填充

class Names():
    obj='world'
    name='python'
print('hello {names.obj} i am {names.name}'.format(names=Names))#输入结果hello world i am python

6.使用魔法参数

args = [‘,’,’inx’]
kwargs = {‘obj’: ‘world’, ‘name’: ‘python’}
print(‘hello {obj} {} i am {name}’.format(*args, **kwargs))#输入结果:hello world , i am python

注意:魔法参数跟你函数中使用的性质是一样的:这里format(*args, **kwargs)) 等价于:format(‘,’,’inx’,obj = ‘world’,name = ‘python’)

7.转义“{}”

print('{{hello}} {{{0}}}'.format('world')) #输出结果:{hello} {world}

跟%中%%转义%一样,format中用 { 来转义{ ,用 } 来转义 }

8.format作为函数变量

name = 'InX'
hello = 'hello,{} welcome to python world!!!'.format #定义一个问候函数
hello(name) #输入结果:hello,inx welcome to python world!!!

9.格式化datetime

from datetime import datetime
now=datetime.now()
print '{:%Y-%m-%d %X}'.format(now) # 输出结果:2017-07-24 16:51:42

10.{}内嵌{}

print('hello {0:>{1}} '.format('world',10)) ##输出结果:hello      world

从最内层的{}开始格式化

11.叹号的用法

print('{!s}国'.format('中')) #输出结果:中国

!后面可以加s r a 分别对应str() repr() ascii() 作用是在填充前先用对应的函数来处理参数

差别就是repr带有引号,str()是面向用户的,目的是可读性,repr()是面向Python解析器的,返回值表示在python内部的含义,ascii (),返回ascii编码


参考链接:

https://blog.csdn.net/u014770372/article/details/76021988


相关文章
|
8月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
464 2
|
8月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
1434 1
|
8月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
392 0
|
9月前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
371 101
|
9月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
478 100
|
9月前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
306 99
|
9月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
627 99
|
9月前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
510 98
|
9月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
8月前
|
存储 Java 索引
(Python基础)新时代语言!一起学习Python吧!(二):字符编码由来;Python字符串、字符串格式化;list集合和tuple元组区别
字符编码 我们要清楚,计算机最开始的表达都是由二进制而来 我们要想通过二进制来表示我们熟知的字符看看以下的变化 例如: 1 的二进制编码为 0000 0001 我们通过A这个字符,让其在计算机内部存储(现如今,A 字符在地址通常表示为65) 现在拿A举例: 在计算机内部 A字符,它本身表示为 65这个数,在计算机底层会转为二进制码 也意味着A字符在底层表示为 1000001 通过这样的字符表示进行转换,逐步发展为拥有127个字符的编码存储到计算机中,这个编码表也被称为ASCII编码。 但随时代变迁,ASCII编码逐渐暴露短板,全球有上百种语言,光是ASCII编码并不能够满足需求
343 4

推荐镜像

更多