python 基础之格式化输出

烈酒焚心 提交于 2020-03-17 05:46:42

字符占位符%s

#_cvvh:"chenxi"
#date:  2019/6/24
print ('chhjg')
# 格式化输出
name = input("Name:")
age = input("age:")
job = input("job:")
salary = input("salary:")

mag = '''
-------------info of  ----
Name: %s
Age: %s
Job: %s
Salary: %s
-----------------end---------
''' % (name, age , job ,salary )  一一对应注意顺序
print(mag)

  测试

D:\python\python.exe D:/untitled/dir/ghg.py
chhjg
Name:chenxi
age:34
job:765
salary:678

-------------info of  ----
Name: chenxi
Age: 34
Job: 765
Salary: 678
-----------------end---------


Process finished with exit code 0

  字符串转换数字

#_cvvh:"chenxi"
#date:  2019/6/24
print ('chhjg')
# 格式化输出
name = input("Name:")
age = int(input("age:"))
job = input("job:")
salary = input("salary:")

mag = '''
-------------info of  ----
Name: %s
Age: %s
Job: %s
Salary: %s
df: %s
-----------------end---------
''' % (name, age , job ,salary ,65-age )  一一对应
print(mag)
测试
chhjg
Name:chen
age:25
job:258
salary:2415

-------------info of  ----
Name: chen
Age: 25
Job: 258
Salary: 2415
df: 40
-----------------end---------

  判断是不是输入的数字

#_cvvh:"chenxi"
#date:  2019/6/24
print ('chhjg')
# 格式化输出
name = input("Name:")
age = int(input("age:"))  
job = input("job:")
salary = input("salary:")
if salary.isdigit():  #判断salary像不像数字,比如200b/200
    salary = int(salary)  #像数字直接转换成数字
else:  #如果输入不是数字就打印mount,并退出
    print("mount")
    exit() #退出程序

mag = '''
-------------info of  ----
Name: %s
Age: %s
Job: %s
Salary: %s
df: %s
-----------------end---------
''' % (name, age , job ,salary ,65-age )
print(mag)
测试-1
D:\python\python.exe D:/untitled/dir/ghg.py
chhjg
Name:cdtfh
age:56
job:hjkhj
salary:85l
mount

Process finished with exit code 0
测试-2
D:\python\python.exe D:/untitled/dir/ghg.py
chhjg
Name:hgt
age:25
job:hghgh
salary:987690

-------------info of  ----
Name: hgt
Age: 25
Job: hghgh
Salary: 987690
df: 40
-----------------end---------


Process finished with exit code 0

%d  数字占位符

#_cvvh:"chenxi"
#date:  2019/6/24
print ('chhjg')
# 格式化输出
name = input("Name:")
age = int(input("age:"))
job = input("job:")
salary = input("salary:")
if  salary.isdigit():
       salary = int(salary)
#else:  #如果输入不是数字就打印mount,并退出
#     print("mount")
#     exit() #退出程序
mag = '''
-------------info of  ----
Name: %s
Age: %s
Job: %s
Salary: %d
df: %s
-----------------end---------
''' % (name, age , job ,salary ,65-age )
print(mag)

  测试-1

D:\python\python.exe D:/untitled/dir/ghg.py
chhjg
Name:tygh
age:68
job:hjhj
salary:jhjhj
Traceback (most recent call last):
  File "D:/untitled/dir/ghg.py", line 22, in <module>
    ''' % (name, age , job ,salary ,65-age )
TypeError: %d format: a number is required, not str

Process finished with exit code 1

  %f精度数字占位符

#_cvvh:"chenxi"
#date:  2019/6/24
print ('chhjg')
# 格式化输出
name = input("Name:")
age = int(input("age:"))
job = input("job:")
salary = input("salary:")
if  salary.isdigit():
       salary = int(salary)
#else:  #如果输入不是数字就打印mount,并退出
#     print("mount")
#     exit() #退出程序
mag = '''
-------------info of  ----
Name: %s
Age: %s
Job: %s
Salary: %f
df: %s
-----------------end---------
''' % (name, age , job ,salary ,65-age )
print(mag)

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!