Python format 用法详解

一、填充字符串

1. 位置

1print("hello {0}, this is {1}.".format("world", "python")) # 根据位置下标进行填充 2print("hello {}, this is {}.".format("world", "python")) # 根据顺序自动填充 3print("hello {0}, this is {1}. {1} is a new language.".format("world", "python")) # 同一参数可以填充多次

输出:

1hello world, this is python. 2hello world, this is python. 3hello world, this is python. python is a new language.

2. key

1obj = "world" 2name = "python" 3print("hello {obj}, this is {name}.".format(obj = obj, name = name))

输出:

hello world, this is python.

3. 列表

1list = ["world", "python"] 2print("hello {names[0]}, this is {names[1]}.".format(names = list))

输出:

hello world, this is python.

4. 字典

1dict = {"obj":"world", "name":"python"} 2print("hello {names[obj]}, this is {names[name]}.".format(names = dict))

输出:

hello world, this is python.

注意:

访问字典的 key,不用引号。

5. 类属性

1class Names(): 2 obj = "world" 3 name = "python" 4 5print("hello {names.obj}, this is {names.name}.".format(names = Names))

输出:

hello world, this is python.

6. 魔法参数

1args = [",", "inx"] 2kwargs = {"obj": "world", "name": "python"} 3print("hello {obj}{} this is {name}.".format(*args, **kwargs))

输出:

hello world, this is python.

注意:

这里的 format(*args, **kwargs) 等价于 format(",", "inx", obj = "world", name = "python")

二、数字格式化

数字

格式

输出

描述

3.1415926

{:.2f}

3.14

保留小数点后两位

3.1415926

{:+.2f}

+3.14

带符号保留小数点后两位

-1

{:+.2f}

-1.00

带符号保留小数点后两位

2.71828

{:.0f}

3

不带小数

5

{:0>2d}

05

数字补零 (填充左边, 宽度为2)

5

{:x<4d}

5xxx

数字补x (填充右边, 宽度为4)

10

{:x<4d}

10xx

数字补x (填充右边, 宽度为4)

1000000

{:,}

1,000,000

以逗号分隔的数字格式

0.25

{:.2%}

25.00%

百分比格式

1000000000

{:.2e}

1.00e+09

指数记法

13

{:>10d}

13

右对齐 (默认, 宽度为10)

13

{:<10d}

13

左对齐 (宽度为10)

13

{:^10d}

13

中间对齐 (宽度为10)

11

'{:b}'.format(11)

1011

二进制

11

'{:d}'.format(11)

11

十进制

11

'{:o}'.format(11)

13

八进制

11

'{:x}'.format(11)

b

十六进制

11

'{:#x}'.format(11)

0xb

十六进制

11

'{:#X}'.format(11)

0xB

十六进制

三、其他用法

1. 转义

print("{{hello}} {{{0}}}".format("world"))

输出:

{hello} {world}

2. format 作为函数变量

1name = "python" 2hello = "hello, welcome to {} world!".format 3print(hello(name))

输出:

hello, welcome to python world!

3. 格式化 datatime

1from datetime import datetime 2now = datetime.now() 3print("{:%Y-%m-%d %X}".format(now))

输出:

2020-12-15 19:46:24

4. {}内嵌{}

print("hello {0:>{1}} ".format("world", 10))

输出:

hello      world

四、参考

python format 用法详解

Python format 格式化函数

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )