2009/2/6 jitendra gupta <jitu.ic...@gmail.com>: > Try this if u r looking for this kind of solution >>>>my_input = "one two three four five six seven eight nine ten" >>>>text = my_input.split() >>>>for i in range(len(text)): > if i+3>=len(text): > print text[i-3:len(text):1] > elif i<=2: > print text[0:i+4] > else: > print text[i-3:i+4]
You can simplify this using the min() and max() functions: for i in range(len(text)): print text[max(0, i-3):min(i+3, len(text))] Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor