python format是什么
python中format主要是用来格式化字符串的。
format用法相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,
并且使用大括号‘{}’作为特殊字符代替‘%’
使用方法由两种:b.format(a)和format(a,b)。
1、基本用法
(1)不带编号,即“{}”
(2)带数字编号,可调换顺序,即“{1}”、“{2}”
(3)带关键字,即“{a}”、“{tom}”
1>>>print('{}{}'.format('hello','world'))#不带字段
2helloworld
3>>>print('{0}{1}'.format('hello','world'))#带数字编号
4helloworld
5>>>print('{0}{1}{0}'.format('hello','world'))#打乱顺序
6helloworldhello
7>>>print('{1}{1}{0}'.format('hello','world'))
2、进阶用法
(1)<(默认)左对齐、>右对齐、^中间对齐、=(只用于数字)在小数点后进行补齐
(2)取位数“{:4s}”、"{:.2f}"等
1>>>print('{}and{}'.format('hello','world'))#默认左对齐
2helloandworld
3>>>print('{:10s}and{:>10s}'.format('hello','world'))#取10位左对齐,取10位右对齐
4helloandworld
5>>>print('{:^10s}and{:^10s}'.format('hello','world'))#取10位中间对齐
6helloandworld
7>>>print('{}is{:.2f}'.format(1.123,1.123))#取2位小数
81.123is1.12
9>>>print('{0}is{0:>10.2f}'.format(1.123))#取2位小数,右对齐,取10位
101.123is1.12
以上内容为大家介绍了python培训之format是什么,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python sorted对键和值进行排序
pythonsorted对键和值进行排序1.概念sorted函数就比sort函数要强大许多了,sort只能对列表进行排序,sorted可以对所有可迭代类型进行排序,并且...详情>>
2023-11-08 23:03:34
python中temp是什么意思?
python中temp是什么意思?当我们在写代码时,为防止系统崩溃等原因,随机记得临时保存是个好习惯。操作过程中想要进行临时保存会放入临时文件夹t...详情>>
2023-11-08 20:44:27
python如何将九九乘法表写入到Excel?
python如何将九九乘法表写入到Excel?现在使用python去输出九九乘法表,已经不再稀奇,我们经常输出的环境是文本,但是今天教大家更为复杂一点的...详情>>
2023-11-08 19:57:53
Python语言中mod表示什么意思
python语言中mod表示取模运算符。语法MOD(a,b)通常情况下取模运算(mod)和求余(rem)运算被混为一谈,因为在大多数的编程语言里,都用'%'...详情>>
2023-11-08 15:27:49热门推荐
python高级函数有几个?
沸python sorted对键和值进行排序
热python中Cartopy是什么
热python time.strptime的格式化
新python中ruamel.yaml模块是什么?
python中temp是什么意思?
python如何将九九乘法表写入到Excel?
python如何使用TemporaryFile()方法创建临时文件?
python怎么安装idle
python如何以换行分割
python3中字符串怎么切片
python如何变为exe程序
python如何处理csv类型文件
python空格怎样设置
技术干货






