x = [1, 2, 3]
print(x[-1]) # --> 3
print(x[-1:]) # --> [3]
输出:
3
[3]
print("abcd"[-1]) # --> "d"
print("abcd"[-1:]) # --> "d"
输出
d
d
The only advantage of using for example s[-1:] or s[:1] with a string instead of s[-1] and s[0] is that the slice expressions will not raise a runtime error when acting on an empty string (something that element access does)… this may allow code simplification:
if len(s) > 0 and s[0] == '*': ...
if s[:1] == '*': ...
参考
https://stackoverflow.com/questions/33841023/is-there-any-difference-between-python-list-slicing-1-and-1