python字符串格式化

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : test04.py
# @Software: PyCharm
# @Author: Journey
# @Date  : 2018/2/26
# @Desc  : test python code

name = input("Name:")
age = input("Age:")
job = input("Job:")
salary = input("Salary:")

print(name,age,job,salary)

msg = '''
-------------info of %s--------------
Name : %s
Age : %s
Job : %s
Salary : %s
---------------------End------------------
''' % (name , name , age , job , salary)

print(msg)
C:\Users\ADMIN\AppData\Local\Programs\Python\Python27\python.exe E:/pythonStudy/day06/demo01/test01.py
Name:'journey'
Age:26
Job:'IT'
Salary:3000
('journey', 26, 'IT', 3000)

-------------info of journey--------------
Name : journey
Age : 26
Job : IT
Salary : 3000
---------------------End------------------


Process finished with exit code 0

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : test04.py
# @Software: PyCharm
# @Author: Journey
# @Date  : 2018/2/26
# @Desc  :

name = input("Name:")
age = input("Age:")
job = input("Job:")
salary = input("Salary:")

if salary.isdigit(): #长得像不像数字,比如200d,'200d'
    salary = int(salary)
else:
    print("must input digit")
    exit() #退出程序

msg = '''
-------------info of %s--------------
Name : %s
Age : %s
Job : %s
Salary : %s
---------------------End------------------
''' % (name , name , age , job , salary)

print(msg)
C:\Users\ADMIN\AppData\Local\Programs\Python\Python27\python.exe E:/pythonStudy/day06/demo01/test02.py
Name:'journey'
Age:26
Job:'IT'
Salary:'30000'

-------------info of journey--------------
Name : journey
Age : 26
Job : IT
Salary : 30000
---------------------End------------------


Process finished with exit code 0

C:\Users\ADMIN\AppData\Local\Programs\Python\Python27\python.exe E:/pythonStudy/day06/demo01/test02.py
Name:'journey'
Age:26
Job:'IT'
Salary:'30000dddd'
must input digit

Process finished with exit code 0

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : test04.py
# @Software: PyCharm
# @Author: Journey
# @Date  : 2018/2/26
# @Desc  :

name = input("Name:")
age = input("Age:")
job = input("Job:")
salary = input("Salary:")

if salary.isdigit(): #长得像不像数字,比如200d,'200d'
    salary = int(salary)
# else:
#     print("must input digit")
#     exit() #退出程序

msg = '''
-------------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(msg)
C:\Users\ADMIN\AppData\Local\Programs\Python\Python27\python.exe E:/pythonStudy/day06/demo01/test02.py
Name:'journy'
Age:26
Job:'IT'
Salary:'300000'

-------------info of journy--------------
Name : journy
Age : 26
Job : IT
Salary : 300000.000000
You will be retired in 39 years
---------------------End------------------


Process finished with exit code 0

 

posted @ 2018-02-26 15:33  journeyIT  阅读(14)  评论(0编辑  收藏  举报