我有一个字符串str = "hello"我想得到str[-3:-1]我得到"ll",但我想得到"llo"(我也想获得最后一个字母,如何获得包含-1的索引)
str = "hello"
str[-3:-1]
"ll"
"llo"
有人可以帮助我,我做错了什么?
尝试一下:-
a = 'hello' print(a[-3:])
输出:-
'llo'
也请避免将变量名用作str,因为它是python中的函数。
str
您将要使用str[-3:]。切片的第二部分是可选的,默认为len(str)。
str[-3:]
len(str)