Python字符串slice()。在我的示例中,使用Python 3.8,当我省略'end'参数时,默认值应为len(string),但不是

问题描述 投票:-1回答:1

此代码:

def reformat_author(self):
    rv = a = self.author
    i0 = a.find(',')
    # One and only one comma:
    if i0 >= 0  and i0 == a.rfind(','):
        t0 = a[0:i0]
        t1 = a[i0+1, len(a)]
        rv = '%s %s' % (t1, t0)
    return rv.strip()

工作正常,将“史密斯,约翰”更改为“约翰·史密斯”。

但是当我改变t1 = a [i0 + 1,len(a)]至t1 = a [i0 + 1]

它没有,尽管doco说:

[从i到j的s切片被定义为索引为k的项序列,使得

i <= k

。 ...如果省略j或无,则使用len。

我误会了吗?

python-3.x string slice
1个回答
-1
投票

愚蠢的问题,请参阅我的评论。

© www.soinside.com 2019 - 2024. All rights reserved.