title: Copy few lines before and after the match string in python
body: <p>I am new to python and trying to write a script for copying 5 lines before and 5 after the matching string is found</p>
  
  <pre><code>import re
  
  text_file = open(input("Input-file name : ") , "r")
  fi =  text_file.readlines()
  
  test = re.compile(r'matching character')
  
  for i in range (len(fi)):
      if test.search(fi[i]):
          print(fi[max(0, i-5)])
          print(fi[max(0, i-4)])
          print(fi[max(0, i-3)])
          print(fi[max(0, i-2)])
          print(fi[max(0, i-1)])
          print(fi[max(0, i-0)])
          print(fi[max(0, i+1)])
          print(fi[max(0, i+2)])
          print(fi[max(0, i+3)])
          print(fi[max(0, i+4)])
  </code></pre>
  
  <p>Is there a better way than adding multiple print statements to get the output in one command.</p>