通过占位符进行格式化输出,常用占位符包括%d,%s和%f
# -*- coding:utf-8 -*-
name = input("input your name: ")
age = input("input your age: ")
if age.isdigit(): # 判断输入的字符串是否同数字形式类似
age = int(age)
else:
print("you should input digit")
exit() # 退出程序
# exit("you should input digit") #退出程序并提示
job = input("input your job: ")
height = int(input("input your height: "))
# 用占位符%d限定 height输入类型,否则报错,而无需使用age中isdigit()进行判断
# 多行字符串输出''' '''内加注释无效,默认为为字符串
msg = '''
------------info of %s-----------
Name: %s
Age : %s
Job : %s
Height : %d
---------------------------------
''' % (name, name, age, job, height) # 用变量填充占位符
print(msg)
运行结果如下所示: