Python字符串最后一个索引(-1),包括在内

问题描述 投票:0回答:2

我有一个字符串str = "hello"我想得到str[-3:-1]我得到"ll",但我想得到"llo"(我也想获得最后一个字母,如何获得包含-1的索引)

有人可以帮助我,我做错了什么?

python string slice
2个回答
1
投票

尝试一下:-

a = 'hello'
print(a[-3:])

输出:-

'llo'

也请避免将变量名用作str,因为它是python中的函数。


0
投票

您将要使用str[-3:]。切片的第二部分是可选的,默认为len(str)

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