Python格式化输出--%s,%d,%f的代码解析
作者:Harry Potter''''s yan 时间:2022-10-31 06:26:44
String(字符型)–%s integer(整形)–%d float(浮点型)–%f
实例
我们需要输出一个人的信息
代码:
#coding=utf-8
#输出一个人的基本信息:姓名:%s 爱好:%s 年龄:%d 岁 身高:%d cm 体重:%f kg
string = "输出一个人的基本信息:姓名:%s 爱好:%s 年龄:%d 岁 身高:%d cm 体重:%f kg"
name1 = "周杰伦"
hobby1 = "basketball"
age1 = 18
height1 = 190
weight1 = 199.36
people1 = string%(name1, hobby1, age1, height1, weight1)
print(people1)
输出结果:
浮点型默认保留六位小数,我们可以设置为保留两位小数,
此时代码变为:
string = "输出一个人的基本信息:姓名:%s
爱好:%s
年龄:%d 岁
身高:%d cm
体重:%.2f kg"`在这里插入代码片`
输出结果:
我们可以再输入一个人试试
代码:
#coding=utf-8
#输出一个人的基本信息:姓名:%s 爱好:%s 年龄:%d 岁 身高:%d cm 体重:%f kg
string = "输出一个人的基本信息:姓名:%s 爱好:%s 年龄:%d 岁 身高:%d cm 体重:%.2f kg"
name1 = "周杰伦"
hobby1 = "basketball"
age1 = 18
height1 = 190
weight1 = 199.36
people1 = string%(name1, hobby1, age1, height1, weight1)
print(people1)
name2 = "林书豪"
hobby2 = "basketball"
age2 = 20
height2 = 200
weight2 = 150
people2 = string%(name2, hobby2, age2, height2, weight2)
print(people2)
输出结果:
来源:https://blog.csdn.net/hanhanwanghaha/article/details/105785257
标签:Python,格式化,输出,%s,%d,%f
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python3美化表格数据输出结果的实现代码
2023-08-28 10:04:03
Mysql双主搭建的方法步骤
2024-01-29 05:57:50
![](https://img.aspxhome.com/file/2023/0/128420_0s.png)
Vue组件之事件总线和消息发布订阅详解
2024-05-02 16:55:42
![](https://img.aspxhome.com/file/2023/0/130150_0s.png)
通过python的matplotlib包将Tensorflow数据进行可视化的方法
2023-04-21 17:37:43
![](https://img.aspxhome.com/file/2023/9/101159_0s.jpg)
基于Python实现简易文档格式转换器
2021-12-12 14:32:31
![](https://img.aspxhome.com/file/2023/9/69549_0s.png)
Python yield生成器和return对比代码实例
2022-07-17 21:54:57
python包合集shutil示例代码详解
2022-03-28 12:04:27
![](https://img.aspxhome.com/file/2023/5/115755_0s.png)
js经验分享 JavaScript反调试技巧
2023-07-13 07:53:04
![](https://img.aspxhome.com/file/2023/7/55927_0s.png)
浅谈Webpack多页应用HMR卡住问题
2023-07-20 01:27:12
![](https://img.aspxhome.com/file/2023/5/55925_0s.jpg)
PyQt5高级界面控件之QTableWidget的具体使用方法
2023-09-05 15:36:41
![](https://img.aspxhome.com/file/2023/0/72240_0s.png)
Python mutiprocessing多线程池pool操作示例
2022-02-11 14:19:46
![](https://img.aspxhome.com/file/2023/8/90308_0s.png)
Python 判断奇数偶数的方法
2023-03-03 18:16:31
golang 各种排序大比拼实例
2024-05-21 10:18:27
详解字典树Trie结构及其Python代码实现
2023-07-07 18:18:41
Django框架教程之正则表达式URL误区详解
2021-03-18 13:46:13
![](https://img.aspxhome.com/file/2023/3/90583_0s.png)
利用Python将数值型特征进行离散化操作的方法
2023-12-02 02:43:36
Python中使用wxPython开发的一个简易笔记本程序实例
2021-09-07 22:06:22
![](https://img.aspxhome.com/file/2023/2/97302_0s.png)
mysql获得60天前unix时间思路及代码
2024-01-29 01:44:07
Centos 6.4源码安装mysql-5.6.28.tar.gz教程
2024-01-25 09:39:58
Python 操作SQLite数据库详情
2024-01-18 04:45:55
![](https://img.aspxhome.com/file/2023/3/78663_0s.jpg)