In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Python格式化输出详情是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
1.%格式化
语法:
%[(name)][flags][width].[precision]typecode
参数:
(name) 可选,用于选择指定的key
flags 可选,对齐方式,可选值为:
+表示右对齐
-表示左对齐。
' '为一个空格,表示在正数的左侧填充一个空格,负数前面加负号。
0 右对齐,正数前面无符号,负数前面加负号,用0填充空白
width 可选 表示显示宽度
.precision 可选 表示小数点后位数
typecode 必选 ,可以选择的参数为:
s 表示字符串
d 表示整数
f 表示浮点数
1.1简单格式化,不使用可选参数print('%s的资产总额为%f元'%('A公司',156261595.89))print('%s的资产总额为%d元'%('A公司',156261595.89))
执行结果:
A公司的资产总额为156261595.890000元
A公司的资产总额为156261595元
1.2 复杂格式化,使用可选参数1.2.1参数 (name),用于选择指定的keyd = {'name':'A公司', 'assets':156261595.89}print("公司名称%(name)s,资产总额为%(assets)f元。" %d)
执行结果:
公司名称A公司,资产总额为156261595.890000元。
1.2.2参数flags和width,对齐方式和宽度d = {'name':'A公司', 'assets':156261595.89}print("公司名称%(name)+6s,资产总额为%(assets)-20f元。" %d)
执行结果:
公司名称 A公司,资产总额为156261595.890000 元。
1.2.3参数.precision 控制小数点后位数d = {'name':'A公司', 'assets':156261595.89345}print("公司名称%(name)+4s,资产总额为%(assets).2f元。" %d)
执行结果:
公司名称 A公司,资产总额为156261595.89元。
2.format格式化
格式化字符串的函数 str.format(),基本语法是通过 {} 和 : 来实现格式化,代替%格式化方法,format函数可以接受不限个参数,位置可以不按顺序。
主要参数释义:
[填充] : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充
[对齐方式与宽度] ^, 分别是居中、左对齐、右对齐,后面带宽度
[正负数显示] + 表示在正数前显示 +,负数前显示 -; (空格)表示在正数前加空格
[数据类型] s 表示字符串 d 表示整数 f 表示浮点数
2.1自定义占位符# 自定义占位符s = '{0}的资产总额为{1}元'print(s.format('A公司','156261595.89'))
执行结果:
A公司的资产总额为156261595.89元
2.2自定义关键字# 自定义关键字s = '{name}的资产总额为{资产}元'print(s.format(name='A公司',资产='156261595.89'))
执行结果:
A公司的资产总额为156261595.89元
2.3填充与对齐(同时使用)# 填充与对齐(同时使用)# :号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充# ^, 分别是居中、左对齐、右对齐,后面带宽度,s = '{name}的资产总额为{资产:*>20}元'print(s.format(name='A公司',资产=156261595.89))
执行结果:
A公司的资产总额为********156261595.89元
2.4数字格式化# 浮点型,保留两位小数s = '{name}的资产总额为{资产:.2f}元'print(s.format(name='A公司',资产=156261595.8988))
执行结果:
A公司的资产总额为156261595.90元
# 浮点型,保留两位小数,并使用千分位分隔符s = '{name}的资产总额为{资产:,.2f}元'print(s.format(name='A公司',资产=156261595))
执行结果:
A公司的资产总额为156,261,595.00元
# 浮点型,保留两位小数,并使用千分位分隔符,表示在正数前显示+,负数前显示-s = '{name}的资产总额为{资产:+,.2f}元'print(s.format(name='A公司',资产=156261595))
执行结果:
A公司的资产总额为+156,261,595.00元
# 百分比,保留两位小数s = '{name}的资产负债率为{资产负债率:.2%}'print(s.format(name='A公司',资产负债率=0.8544))
执行结果:
A公司的资产负债率为85.44%
3.f-String格式化
Python格式化字符串f-string,采用f'{content:format}' 设置字符串格式,其中 content 是替换并填入字符串的内容,可以是变量、表达式或函数等。采用默认格式时不必指定 {:format},只写 {content} 即可。
3.1简单使用# 简单使用name = 'A公司'assets = 156261595msg = f'{name}的资产总额为{assets}元。'print(msg)
执行结果:
A公司的资产总额为156261595元。
3.2复杂控制# {content:format} 格式化使用name = 'A公司'assets = 156261595msg = f'{name}的资产总额为{assets:,.2f}元。'print(msg)
执行结果:
A公司的资产总额为156,261,595.00元。
注释:填充与对齐以及对数值的处理,与format函数格式化类似,参照format部分的讲解。
关于Python格式化输出详情是怎样的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.