python字符串格式化的三種方式

今天忽然想了解一下字符串的格式化的方法,總結了一下:html

第一種:是用%s 和%d來格式化字符串

 

name = input("name:") age = int(input("age:")) salary = int(input("salary:")) job = input("job:") """ info = ''' ===============info of %s============== name: %s age: %d salary: %d job: %s ''' % (name,name,age,salary,job) print(info)

 

第二種:

name = input("name:") age = int(input("age:")) salary = int(input("salary:")) job = input("job:") info2 = ''' ===============info of {_name}============= name: {_name} age: {_age} salary: {_salary} job: {_job} '''.format(_name = name, _age = age, _salary=salary, _job=job) print(info2)

第三種:

name = input("name:") age = int(input("age:")) salary = int(input("salary:")) job = input("job:") info3 = ''' ============info of {0}================= name: {0} age: {1} salary: {2} job: {3} '''.format(name, age, salary, job) print(info3)

 

參考:https://www.cnblogs.com/chuckzeng/p/8010699.htmlweb

相關文章
相關標籤/搜索