Python format 用法详解

一、填充字符串

1. 位置

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

输出:

hello world, this is python.
hello world, this is python.
hello world, this is python. python is a new language.

2. key

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

输出:

hello world, this is python.

3. 列表

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

输出:

hello world, this is python.

4. 字典

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

输出:

hello world, this is python.

注意:

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

5. 类属性

class Names():
    obj = "world"
    name = "python"

print("hello {names.obj}, this is {names.name}.".format(names = Names))

输出:

hello world, this is python.

6. 魔法参数

args = [",", "inx"]
kwargs = {"obj": "world", "name": "python"}
print("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 作为函数变量

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

输出:

hello, welcome to python world!

3. 格式化 datatime

from datetime import datetime
now = datetime.now()
print("{:%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 格式化函数

全部评论

相关推荐

03-06 12:44
已编辑
吉林大学 Java
是个千人厂,没听过名字。1.&nbsp;做一个自我介绍。2.&nbsp;你这个项目和技术栈从哪里学的?有报辅导班嘛[答&nbsp;都是是自己网上学的,学校教的东西没用]3.&nbsp;我看了你放在github上的项目,前端也是你写的嘛[答&nbsp;AI写的,90%精力用于后端开发,前端单纯用于作为后端逻辑的可视化技术验证(骗你的其实后端也是AI写的)]4.&nbsp;好,你觉得这些技术栈研究得最深刻的是哪个[答&nbsp;八股压根没背到后面,昨晚背了MySQL就说MySQL]5.&nbsp;那讲一下MySQL的索引[答&nbsp;从B+树选型一路吟唱到联合索引,索引失效]6.&nbsp;联合索引ABC问题,AB走索引嘛,BC走索引嘛?BAC走索引嘛?A&nbsp;or&nbsp;B&nbsp;走索引嘛[走,不走,走,不走。面试官点头说可以]7.&nbsp;讲一下项目里Redission分布式锁实现8.&nbsp;Watchdog机制具体是怎么工作9.&nbsp;消息队列有考虑过Kafka嘛,怎么选型的10.&nbsp;你这个项目消息队列可能出现什么问题,怎么解决这个问题?[瞎扯没用的,被面试官引导答了视频处理可能产生消息堆积问题,然后开始吟唱]11.&nbsp;文件分片自己写的还是用的什么框架?上传进度的Redis数据结构?上传的视频有多大?小分片大小?12.&nbsp;项目里Redis会话记忆是啥意思?[面试官说不行,没人把这个全放Redis里[生气R]]13.&nbsp;那这和直接查数据库有什么区别[扯了Token成本和解决幻觉问题之类的,给面试官听笑了,我最后也没绷住]14.&nbsp;你平时是怎么使用AI&nbsp;coding的15.&nbsp;算法,给了我一个leedcode链接,一看做过了。然后换了一道三数之和,也做过了。然后面试官说算了,让我讲讲思路吧反问:1.有什么需要提高的地方2.介绍一下部门业务有哪些这个面试官真的感官非常非常好,问问题还疯狂引导,感觉不会也会了。找实习&nbsp;&nbsp;牛客AI配图神器#
查看15道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务