With string indices, is there a way to slice to end of string without using len()? Negative indices start from the end, but [-1] omits the final character.
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?
Or even:
Are you looking for this?
I found myself needing to specify the end index as an input variable in a function. In that case, you can make
end=None
. For example:You could always just do it like this if you want to only omit the first character of your string:
Here you are specifying that you want the characters from index 1, which is the second character of your string, till the last index at the end. This means you only slice the character at the first index of the string, in this case 'H'. Printing this would result in: 'elp'
Not sure if that's what you were after though.
Yes, of course, you should:
You can instead try using: