小编Vik*_*nti的帖子

为什么python字符串中的负索引没有按预期结果?

我在python中创建了一个字符串:'HelpA'并将其标记为单词.

>>>word='HelpA'

>>>word

'HelpA' #expected string

>>>word[0:4]

'Help' #expected string
Run Code Online (Sandbox Code Playgroud)

在使用负索引调用单词时,我得到的是完整字符串而不是预期的部分字符串.

>>>word[-97:]

'HelpA'  #not the expected string
Run Code Online (Sandbox Code Playgroud)

它给出了整个字符串而不是预期的字符串:'pA'为什么会发生这种情况?

python string indices

-1
推荐指数
1
解决办法
73
查看次数

标签 统计

indices ×1

python ×1

string ×1