python---------字符串格式化format

print("model saved to {}".format("save_path"))
print("model saved to %s" % "save_path")
## model saved to save_path
## model saved to save_path
### 普通的形式
'my name is {} ,age {}'.format('hoho',18)
'my name is hoho ,age 18'
### 列表*li
li = ['hoho',18]
'my name is {} ,age {}'.format(*li)
'my name is hoho ,age 18'
### 字典**hash
hash = {'name':'hoho','age':18}
'my name is {name},age is {age}'.format(**hash)
'my name is hoho,age is 18'
### 迭代器range(1,6)==[1,2,3,4,5]
fname = ['my.{}.jpg'.format(i) for i in range(5)]
print(fname)
['my.0.jpg', 'my.1.jpg', 'my.2.jpg', 'my.3.jpg', 'my.4.jpg']

 

你可能感兴趣的:(Python基础)