As was mentioned in the comments, myPattern is simply a string. The 'r' simply tells Python that it's a "raw" string and shouldn't attempt to interpret escaped text.
Once you have your regular expression formatted the way you like it (note, since you named the parameter "linhead", you'll need to identify it in the arguments to format:
myPattern = r'\s*{linehead}'.format(linehead="someText")
You need to make an regular expression object with re.compile().
myRegex = re.compile(myPattern)
You can now use myRegex as you originally intended:
if myRegex.search(l) is not None:
...
Or, you can do this all in one go as @IceArdor suggested:
myPattern = re.compile(r'\s*{linehead}'.format(linehead="someText");
See https://docs.python.org/2/library/re.html#module-re
myPattern = r'\s*{linehead}'.format(linehead="someText")if myPattern.search(l) is not None:, I'm gettingAttributeError: 'str' object has no attribute 'search'myPattern = re.compile(r'\s*{linehead}'.format(linehead="someText");myPattern.search(l) is not None