Detecting String Pattern Using Python