引入:程序中会有按某种特定格式输出内容的需求,需要用到占位符
--------------- 完美的分割线 -----------------
1.1.格式化输出的需求
程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式
比如要求用户输入用户名和年龄,然后打印如下格式:
My name is xxx,my age is xxx.
很明显,用逗号进行字符串拼接,只能把用户输入的名字和年龄放到末尾,无法放到指定的xxx位置,而且数字也必须经过str(数字)的转换才能与字符串进行拼接。
这就需要用到占位符,常用的占位符有如:%s,%d,%f
# 一般输出结果这样
xiaoming 25 it 5000
# 好看的输出结果(格式化输出)
------ info of xiaoming ------ Name:xiaoming Age:25 Job:it Salary:5000 ------------ END -------------
1.2.占位符的类型
1)%s字符串占位符
可以接收字符串,也可接收数字
print('My name is %s,my age is %s' %('xiaoming',18)) --->My name is xiaoming,my age is 18
2)%d数字占位符
只能接收整数,数字;
print('My name is %s,my age is %d' %('xiaoming',18)) --->My name is xiaoming,my age is 18 print('My name is %s,my age is %d' %('xiaoming','18')) ---># 报错如下: Traceback (most recent call last): My name is xiaoming,my age is 18 File "D:/02.PyProjects/1.svndata/01.Python基础/00.Python/Python格式化输出.py", line 129, in <module> print('My name is %s,my age is %d' %('xiaoming','18')) TypeError: %d format: a number is required, not str
3)%f浮点数,小数
可以保留6位小数的精度
总结:
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") # 退出程序
1.3.格式化输出的方法
实例演示:格式化输出指定内容
1)方法1:占位符,检测验证数据类型
print("以下内容为:占位符的应用") name = input("name:") age = int(input("age:")) job = input("job:") salary = int(input("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(info1)
输入返回如下:
以下内容为:占位符的应用 name:xiaoming age:22 job:IT salary:10000 ------ info of xiaoming ------ Name:xiaoming Age:22 Job:IT Salary:10000.000000 You will be retired in 43 years. ------ END ------
2)方法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)
返回值如下:
以下内容为:字符串拼接的应用 name:xiaoming age:22 job:IT salary:10000 ==== info of ''' + name +''' ==== Name:''' + name + ''' Age:''' + age + ''' Job:''' + job + ''' Salary:''' + salary + ''' ==== END ====
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:22 job:IT salary:10000 ------ info of xiaoming ------ Name:xiaoming Age:22 Job:IT Salary:10000
4)格式化输出方法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} '''.format(name,age,job,salary) print(info4)
输入及返回如下:
name:xiaoming age:22 job:IT salary:10000 ==== info of xiaoming ==== Name:xiaoming Age:22 Job:IT Salary:10000
==== END ====
5)实例练习
name = input("Name:") age = int(input("Age:")) job = input("Job:") salary = input("Salary:") if salary.isdigit(): salary = int(salary) else: exit("you must input digit for Salary") print(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(info1)
加入了数据类型的判断。
---- 完毕,呵呵呵呵 ----