-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphone_format.py
More file actions
36 lines (33 loc) · 899 Bytes
/
phone_format.py
File metadata and controls
36 lines (33 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def phone_format(s):
"""Format tel number (using f-str)"""
l = len(s)
if l < 3:
return s
elif l < 5:
return f"{s[-4:-2]}-{s[-2:]}"
elif l < 8:
return f"{s[-7:-4]}-{s[-4:-2]}-{s[-2:]}"
elif l < 10:
return f"{s[-9:-7]} {s[-7:-4]}-{s[-4:-2]}-{s[-2:]}"
elif l == 10:
if s[0] == '9':
return f"{s[-10:-7]} {s[-7:-4]}-{s[-4:-2]}-{s[-2:]}"
else:
return f"({s[-10:-7]}){s[-7:-4]}-{s[-4:-2]}-{s[-2:]}"
else:
return f"{s[:-10]} {s[-10:-7]} {s[-7:-4]}-{s[-4:-2]}-{s[-2:]}"
for s in ["+839987654321",
"+73437654321",
"9987654321",
"3437654321",
"87654321",
"7654321",
"Lizard",
"654321",
"54321",
"4321",
"321",
"21",
"1",
]:
print(phone_format(s).rjust(20))