Python-格式化输出

Python-格式化输出

1.需求引入

  • 普通的输出方式:比如要求用户输入用户名和年龄,然后打印如下格式
  • 很明显,用逗号进行字符串拼接,只能把用户输入的名字和年龄放到末尾,无法放到指定的位置
  • 而且数字也必须经过 str (数字)的转换才能与字符串进行拼接。
My name is xiaoming ,my age is 25
  • 比较秀的效果:格式化输出结果
------ info of xiaoming ------
Name:    xiaoming
Age:     25
Job:     it
Salary:  5000
------ END ------

2.占位符的类型

  • %s 字符串占位符
  • %d 数字占位符
  • %f 浮点数

2.1.字符串占位符 %s

  • %s 可以接收字符串,也可接收数字
print('My name is %s , my age is %s' %('xiaoming',25))
----------------------------
My name is xiaoming , my age is 25
----------------------------

2.2.数字占位符 %d

  • %d 只能接收整数,数字
print('My name is %s , my age is %d' %('xiaoming',25))
----------------------------
My name is xiaoming , my age is 25
----------------------------

# 错误的示例:这里的 25 是字符串类型
print('My name is %s,my age is %d' %('xiaoming','25'))
----------------------------
Traceback (most recent call last):
  File "d:/workspace/zsdocs/运维开发/python.zs/python_docs/python_base/test.py", line 9, in <module>
    print('My name is %s,my age is %d' %('xiaoming','25'))
TypeError: %d format: a number is required, not str
----------------------------

2.3.浮点数-小数 %f

  • %f 可以保留 6 位小数的精度

2.4.注意点

  • 1.需要预置某些变量的数据类型,防止用户输入错误类型的数据,例如,如果是数字可能需要转换数据类型为 int,便于可以进行一些运算,如 age 和 salary 应该是数字
  • 2.如果没有在前面判断输入的数据的类型,可以使用占位符来规范数据的类型,例如,占位符改成 %d 则 input 时只能输入数字,否则会报错
  • 3.%s 可以接收数字,但是会存储为字符串类型,可以对其进行数据类型的转换,如果需要先检测输入的是否为数字,可以用 isdigit() 方法
---------------------------
if salary.isdigit():
    salary = int(salary)        # 转换为数字类型
else:
    print("you must be input digit for Salary")
    exit("bye bye")             # 退出程序
---------------------------

3.常用的格式化输出方法

3.1.占位符方法-常用

  • 实例演示:占位符
print("格式化输出-占位符方法")
name = input("name:")
age = int(input("age:"))
job = input("job:")
salary = int(input("salary:"))

if salary.isdigit():
    salary = int(salary)
else:
    exit("you must input digit for Salary")

print("普通格式输出:\n",name,age,job,salary)

info1 = '''
==== info of %s ====
Name: %s
Age: %d
Job: %s
Salary: %f
You will be retired in %s years.
==== END ====
'''%(name,name,age,job,salary,65-age)
print("格式化输出:\n",info1)
  • 以下为输出内容
格式化输出-占位符方法
Name:xiaoming
Age:25
Job:it
Salary:8000
普通格式输出:
 xiaoming 25 it 8000
格式化输出:
 ==== info of xiaoming ====
Name: xiaoming
Age: 25
Job: it
Salary: 8000.000000
You will be retired in 40 years.
==== END ====

3.2.字符串拼接方法-不常用

  • 实例演示:字符串拼接
---------------------------
print("格式化输出-字符串拼接的应用")
name = input("name:")
age = int(input("age:"))
job = input("job:")
salary = input("salary:")
info2 = """
==== info of ''' + name +''' ====
Name:''' + name + '''
Age:''' + age + '''
Job:''' + job + '''
Salary:'''+ salary + '''
==== END ====
"""
print(info2)
---------------------------

3.3.format() 方法-常用

  • 实例演示
name = input("name:")
age = int(input("age:"))
job = input("job:")
salary = input("salary:")
info3 = '''
==== info of {_name} ====
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
==== END ====
'''.format(_name=name,
          _age=age,
          _job=job,
          _salary=salary)
print(info3)
  • 以下为输出内容
name:xiaoming
age:25
job:it
salary:8000

==== info of xiaoming ====
Name:xiaoming
Age:25
Job:it
Salary:8000
==== END ====

3.4.变量替换方法-不推荐使用

  • 因为变量定义比较模糊
  • 实例演示
name = input("name:")
age = int(input("age:"))
job = input("job:")
salary = input("salary:")
info4 = '''
==== info of {0} ====
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
==== END ====
'''.format(name,age,job,salary)
print(info4)
  • 以下为输出内容
name:xiaoming
age:25
job:it
salary:8000

==== info of xiaoming ====
Name:xiaoming
Age:25
Job:it
Salary:8000
==== END ====
posted @ 2017-12-18 19:11  天生帅才  阅读(1504)  评论(0编辑  收藏  举报
// 百度统计