1] mean/do in python?
This question already has an answer here:
It slices the string to omit the last character, in this case a newline character:
>>> 'testn'[:-1]
'test'
Since this works even on empty strings, it's a pretty safe way of removing that last character, if present:
>>> ''[:-1]
''
This works on any sequence, not just strings.
It means "all elements of the sequence but the last". In the context of f.readline()[:-1]
it means "I'm pretty sure that line ends with a newline and I want to strip it".
It selects all but the last element of a sequence.
Example below using a list:
In [15]: a=range(10)
In [16]: a
Out[16]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [17]: a[:-1]
Out[17]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
链接地址: http://www.djcxy.com/p/26732.html
上一篇: list [x :: y]是做什么的?
下一篇: 1]意思/在python中做什么?