<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def finalize_filename(filename, file_format=None):
 """ Replaces invalid characters in filename string, adds image extension and reduces filename length :param filename: Incomplete filename string :type filename: str :param file_format: Format which will be used for filename extension :type file_format: MimeType :return: Final filename string :rtype: str """

        for char in [' ', '/', '\\', '|', ';', ':', '\n', '\t']:
            filename = filename.replace(char, '')

        if file_format:
            suffix = str(file_format.value)
            if file_format.is_tiff_format() and file_format is not MimeType.TIFF:
                suffix = str(MimeType.TIFF.value)
                filename = '_'.join([filename, str(file_format.value).replace(';', '_')])

            filename = '.'.join([filename[:254 - len(suffix)], suffix])

        LOGGER.debug("filename=%s", filename)

        return filename