Python Basic - Python格式化输出

时间:2020-04-19
本文章向大家介绍Python Basic - Python格式化输出,主要包括Python Basic - Python格式化输出使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

功能解释

一句话概括:数据更美观,更易懂
有时候需要将特定的数据,按照人更友好的格式进行输出,便于查看或存储,例如,需要指定输出某个人的信息,如,姓名,年龄,公司,部门,职位,工资等信息,按照特定的格式输出后更易读取。

代码示例

[root@Private python-script]# cat formatout.py

#!/usr/local/bin/python3
#author :       feihuang
#date   :       2020-04-19

name = str(input("Your name:"))
age = int(input("Your age:"))
company = str(input("Your company:"))
department = str(input("Your department:"))
position = str(input("Your position:"))
salary = int(input("Your salary:"))
jb = str(input("Your job description:"))




outputmsg = '''
***********************Personal Information for %s***************************
*       Your Name                               :       %s
*       Your Age                                :       %d
*       Your Company                            :       %s
*       Your Department                         :       %s
*       Your position                           :       %s
*       Your Salary                             :       %d
*       Your Job description                    :       %s
*
****************************************************************************
'''% (name,name,age,company,department,position,salary,jb)
print(outputmsg)

#============================以下为输出结果================================
'''

ot@Private python-script]# ./formatout.py 
Your name:Fei Huang
Your age:30
Your company:MI
Your department:IT
Your position:employer
Your salary:8000
Your job description:Move brick

***********************Personal Information for Fei Huang*******************
*       Your Name                               :       Fei Huang
*       Your Age                                :       30
*       Your Company                            :       MI
*       Your Department                         :       IT
*       Your position                           :       employer
*       Your Salary                             :       8000
*       Your Job description                    :       Move brick
*
****************************************************************************
'''

常见格式化占位符

%s string字符串
%d digit 数字
%f float 浮点数

原文地址:https://www.cnblogs.com/fei-huang/p/12731919.html