My friends, if you need to reprint please indicate the source: juejin.cn/post/699176…

Disclaimer: During the teaching of artificial intelligence technology, many students asked me some python related questions, so in order to let students master more extended knowledge and better understand AI technology, I asked my assistant to share this Python series of tutorials, hoping to help you! Since this Python tutorial is not written by me, it is not as funny and boring as my AI teaching. But its knowledge points or say in place, also worth reading! PS: if you don’t understand this article, please read the previous article first. Step by step, you won’t feel difficult to learn a little every day!

Like % format expressions, format calls can be made more complex to support more advanced uses. For example, a format string can specify object attributes and dictionary keys. In the first example below, index the key “spam” on the dictionary, and then get the “platform” property from the sys module object that you have imported. The second example does the same thing, but this time the object is specified by keyword instead of location:

>>> Import sys >>> 'My {1[spam]} runs {0.platform}'. Format (sys,{'spam': 'laptop'}) 'My laptop runs win32' >>> 'My {config[spam]} runs {sys.platform}'. Format (sys=sys, config={'spam': 'laptop'}) My laptop runs win32'Copy the code

Square brackets in the format string can specify list offsets to perform the index, but only single positive offsets are supported. To specify negative offsets or sharding, the expression must be run outside of the format string. Here are a few examples:

> > > somelist = list (' SPAM ') > > > somelist [' S ', 'P', 'A', 'M'] > > > 'first = [0] {0}, third = [2] {0}'. The format (somelist) 'first = S, third = A > > >' first = {0}, last = {1} '. The format (somelist [0], somelist [1]) # [1] fails in FMT 'first = S, the last = M > > > Parts = somelist[0],somelist[-1],somelist[1:3] # [1:3] Fails in FMT >>> 'first={0},last={1},middle={2}'. Format (*parts) "first=S,last=M,middle=['P','A']"Copy the code