字符串格式化输出定义

1.加号拼接:

name = input("name:")
age = input("age:")
job = input("job:")

message = '''
---------information of ''' + name +'''----------
name:''' + name + '''
age:''' + age + '''
job:''' + job

print(message)

----------information of zhoumingkang----------
name:zhoumingkang
age:30
job:it
Process finished with exit code 0

2.%s格式化字符串

name = input("name:")
age = int(input("age:"))     #强制将字符串转为×××
print(type(age))             #打印变量类型,以确定是否正确
job = input("job:")
message1 = '''----------information of %s----------
name:%s
age:%d             #%d代表整型
job:%s
''' % (name,name,age,job)
print(message1)

3.{}格式化字符串

name = input("name:")
age = input("age:")
job = input("job:")
message2 = '''----------information of {_name}----------
name:{_name}
age:{_age}
job:{_job}
'''.format(_name=name,_age=age,_job=job)
还有另外一种大括号格式化字符串方法:
message3 = '''----------information of {0}----------
name:{0}
age:{1}
job:{2}
'''.format(name,age,job)
print(message3)

备注:

当使用‘’‘%s’‘’时,后面接在用%(变量名)
当使用‘’‘{_name}’‘’时,后面接在用 .format(_name=name)

4.简单的格式化字符串输出

name = input("name:")
age = input("age:")
job = input("job:")
print('my name is {0}, age is  {1},job is {2}'.format(name,age,job))
输入结果:
name:kang
age:30
job:it
my name is kang, age is  30,job is it