python字符串的格式化

# -*- coding:utf-8 -*-
"""
@Author:janson
@Date:2018/8/1
@File:StrFormat.py
"""

#实现字符串的格式化:三种方式(字符串拼接、%s%d%f占位符、format())

# python2.x 用raw_input()   python3.x直接用input()  忘记python2.x里的input用法
name = raw_input('请输入姓名:')
#age = raw_input('年龄:')
age = int(raw_input('年龄:'))
job = raw_input('工作:')
salary = int(raw_input('薪水:'))

info1 = '''
------------info of '''+ name +'''----------------
Name: '''+ name + '''
Age:'''+ age + '''
Job:'''+ job + '''
Salary:'''+ salary + '''
'''
print info1

info2=''''
------------info of %s----------------
Name:%s
Age:%d
Job:%s
Salary:%d
'''%(name,name,age,job,salary)
print info2

info3='''
------------info of {_name}----------------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
'''.format(_name=name,_age=age,_job=job,_salary=salary)
print info3

 

posted @ 2019-02-13 17:57  那心之所向  阅读(126)  评论(0编辑  收藏  举报