Python--format()学习记录
发布日期:2021-07-01 04:21:08
浏览次数:2
分类:技术文章
本文共 791 字,大约阅读时间需要 2 分钟。
.format():格式化输出字符串
示例:
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'))) #调用方法
输出:
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
增补知识点:
填充与对齐
填充常跟对齐一起使用^、<、>分别是居中、左对齐、右对齐,后面带宽度:号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充比如In [15]: '{:>8}'.format('189')Out[15]: ' 189'In [16]: '{:0>8}'.format('189')Out[16]: '00000189'In [17]: '{:a>8}'.format('189')Out[17]: 'aaaaa189'
欢迎留言交流!
转载地址:https://mtyjkh.blog.csdn.net/article/details/79763695 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月23日 01时34分05秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
x265-1.7版本-common/scalinglist.cpp注释
2019-05-02
x265-1.7版本-common/slice.cpp注释
2019-05-02
x265-1.7版本-common/slice.h注释
2019-05-02
x265-1.7版本-encoder/bitcost.h注释
2019-05-02
x265-1.7版本-encoder/dpb.cpp注释
2019-05-02
x265-1.7版本-encoder/dpb.h注释
2019-05-02
x265-1.7版本-encoder/encoder.cpp注释
2019-05-02
x265-1.7版本-encoder/encoder.h注释
2019-05-02
x265-1.7版本-encoder/frameencoder.cpp注释
2019-05-02
x265-1.7版本-encoder/frameencoder.h注释
2019-05-02
x265-1.7版本-encoder/motion.cpp注释
2019-05-02
高阶函数
2019-05-02
继承和多态
2019-05-02
获取对象信息
2019-05-02
实例属性和类属性
2019-05-02
使用__slots__
2019-05-02
使用@property
2019-05-02
多重继承
2019-05-02
定制类
2019-05-02
使用枚举类
2019-05-02