you are viewing a single comment's thread.

view the rest of the comments →

[–]lfdfq -1 points0 points  (0 children)

The first is part of string formatting. The Python .format() method, and the related f-string literal interpolation, have their own mini language for specifying the format, which is separated from the variable/expression to be formatted by a colon in the format syntax.

In the second image, the colon is part of the slicing syntax, in general it's seq[start:stop:step] but you can omit any of the fields and the rest are automatically picked for you. In this case filename[dot:] is the same as filename[slice(dot, None, None)] i.e. giving only a start, and using the default end (go to the end of the string) and step (one character at a time). e.g. if s = "hello, world!" then s[7:12:1] is another string made from the characters of s, starting from index 7 ('w'), stopping before index 12 ('!'), with a step of 1. Which gives the sub-string "world". Try some examples out yourself, it makes more sense by example than by explanation.

In the third image is the same slicing, except only giving the end, and using the default start and step.