Printable International Bank Account Number (IBAN) as specified in ISO
    13616.

    :param number: string

    >>> iban('BE43068999999501')
    'BE43 0689 9999 9501'
    >>> iban('XY32012341234123', validate=True)
    Traceback (most recent call last):
        ...
    ValueError: Invalid IBAN, country unknown
    >>> iban('BE43068999999502', validate=True)
    Traceback (most recent call last):
        ...
    ValueError: Invalid IBAN, digits check failed

def iban(number, validate=False):
    '''
    Printable International Bank Account Number (IBAN) as specified in ISO
    13616.

    :param number: string

    >>> iban('BE43068999999501')
    'BE43 0689 9999 9501'
    >>> iban('XY32012341234123', validate=True)
    Traceback (most recent call last):
        ...
    ValueError: Invalid IBAN, country unknown
    >>> iban('BE43068999999502', validate=True)
    Traceback (most recent call last):
        ...
    ValueError: Invalid IBAN, digits check failed

    '''

    number = bban_compact(number)
    if validate:
        country = number[:2]
        if country not in BBAN_RULES:
            raise ValueError(_('Invalid IBAN, country unknown'))

        # Do the 10-mod-97 check
        digits = bban_base10(number)
        if int(digits) % 97 != 1:
            raise ValueError(_('Invalid IBAN, digits check failed'))

        # Check BBAN for country
        bban(number[4:], country, validate=True)

    groups = [number[x:x + 4] for x in range(0, len(number), 4)]
    return ' '.join(groups)