I would like to add two points to the discussion:
You can use
Noneinstead on an empty space to specify "from the start" or "to the end":'abcde'[2:None] == 'abcde'[2:] == 'cde'This is particularly helpful in functions, where you can't provide an empty space as an argument:
def remove_from_tailsubstring(s, n_char=0start, end): """Remove `n``start` characters from the beginning and `end` characters from the end of string `s`. Examples -------- >>> remove_from_tailsubstring('abcde', 0, 3) 'abcde''abc' >>> remove_from_tailsubstring('abcde', 21, None) 'abc''bcde' """ return s[s[start:-n_char if n_char > 0 else None]end]Python has slice objects:
idx = slice(2, None) 'abcde'[idx] == 'abcde'[2:] == 'cde'