<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def prepare_string(partname, thread, maxw):
 """ extract a content string for part 'partname' from 'thread' of maximal length 'maxw'. """

    # map part names to function extracting content string and custom shortener
    prep = {
        'mailcount': (prepare_mailcount_string, None),
        'date': (prepare_date_string, None),
        'authors': (prepare_authors_string, shorten_author_string),
        'subject': (prepare_subject_string, None),
        'content': (prepare_content_string, None),
    }

    s = ' '  # fallback value
    if thread:
        # get extractor and shortener
        content, shortener = prep[partname]

        # get string
        s = content(thread)

    # sanitize
    s = s.replace('\n', ' ')
    s = s.replace('\r', '')

    # shorten if max width is requested
    if maxw:
        if len(s) > maxw and shortener:
            s = shortener(s, maxw)
        else:
            s = s[:maxw]
    return s