Lecture Notes on 25 Oct 2013 Substrings You can slice a string into substrings. Consider the string st = 'Life of Brian' The generic way of getting the substring from the above string would be to get a slice like so: st [startIndex : endIndex] The substring will start at the startIndex but will end at endIndex - 1. One way of remembering this is (endIndex - startIndex) is the length of the substring. Here are four variations of getting the substring s = st[5:7] # s = 'of' s = st[:4] # s = 'Life' s = st[8:] # s = 'Brian' s = st[:] # s = 'Life of Brian' If the startIndex is missing, then it implies the substring will start at the beginning of the string. If the endIndex is missing, then it implies the substring will go all the way to the end of the string. If both the startIndex and the endIndex are missing, then you get a copy of the string. # Code to generate all substrings of a given string def main(): st = input ('Enter a string: ') len_str = len(st) window = len_str while (window > 0): startIndex = 0 while (startIndex + window <= len_str): print (st[startIndex : startIndex + window]) startIndex += 1 window = window - 1 main()