def strip_trailing_period(s):
    """
    Return the `s` string with trailing periods removed when needed.
    """
    if not s:
        return s
    s = s.strip()
    if not s.endswith('.'):
        return s
    if len(s) < 3:
        return s
    if s[-2].isupper():
        return s
    if s[-3] == '.':
        return s
    if s.lower().endswith(('inc.', 'corp.', 'ltd.', 'llc.', 'co.', 'llp.')):
        return s
    return s.rstrip('.')
