name = input("name:") age = input("age:") job = input("job:") salary = input("salary:") info1 = ''' ---------------------info of %s-------------------- Name:%s Age:%s Job:%s Salary:%s ''' % (name,name,age,job,salary) info2 = ''' ---------------------info of {_name}-------------------- Name:{_name} Age:{_age} Job:{_job} Salary:{_salary} '''.format(_name=name,_age=age,_job=job,_salary=salary) info3 = ''' ---------------------info of ''' + name + '''-------------------- Name:''' + name +''' Age:''' + age info4 = ''' ---------------------info of {0}-------------------- Name:{0} Age:{1} Job:{2} Salary:{3} '''.format(name,age,job,salary) print(info1) print(info2) print(info3) print(info4)
字符串的格式化的以上三种方式。
1)使用+拼接模式
2)使用占位符方式 %s 字符串占为符,%d数字占为符
3)使用字符串类的格式化方法,即 str.format()
优缺点:
1)此等方式开辟内存空间较大,不好,弃用
2)此等方式方便,可用
3)字符串格式化方法也挺好,可用。