num = '123456789'

def fmt(num):
    block1 = list(num[:4])
block2 = len(num) > 4 and [num[4:6]] or []
block3 = len(num) > 6 and [num[6:]] or []
return '.'.join(block1 + block2 + block3)
print(fmt(num))
Format number number with specific mask regex python