使用字符串索引,有没有办法切片到字符串结尾而不使用len()?负指数从结尾开始,但[-1]省略了最终字符.
word = "Help"
word[1:-1] # But I want to grab up to end of string!
word[1:len(word)] # Works but is there anything better?
Run Code Online (Sandbox Code Playgroud)
Uku*_*kit 32
您可以尝试使用:
word[1:]
Run Code Online (Sandbox Code Playgroud)
Bob*_*ley 11
我发现自己需要将结束索引指定为函数中的输入变量.在这种情况下,你可以做end=None.例如:
def slice(val,start=1,stop=None)
return val[start:stop]
word = "Help"
slice(word) # output: 'elp'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60396 次 |
| 最近记录: |