-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.py
74 lines (53 loc) · 1.93 KB
/
format.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
## How to clean up user's inputs
# name = input("What's your name? ").strip()
# if "," in name:
# last, first = name.split(", ")
# name = f"{first} {last}"
# print(f"hello, {name}")
# ## However, the code still needs work becuase it does not consider other potential issues
# import re
# name = input("What's your name? ").strip()
# matches = re.search(r"^(.+), (.+)$", name)
# if matches:
# last, first = matches.groups()
# name = f"{first} {last}"
# print(f"hello, {name}")
# ## Similarly you can approach the above code by:
# import re
# name = input("What's your name? ").strip()
# matches = re.search(r"^(.+), (.+)$", name)
# if matches:
# last = matches.group(1)
# first = matches.group(2)
# print(f"hello, {name}")
# ## Similarly to code above, we can concantinate
# import re
# name = input("What's your name? ").strip()
# matches = re.search(r"^(.+), (.+)$", name)
# if matches:
# name = matches.group(2) + " " + matches.group(1)
# print(f"hello, {name}")
# ## Furthermore we can add a '?' to tolerate commas and no spacing from user
# ## i.e. if a user were to input "Malan,David"
# import re
# name = input("What's your name? ").strip()
# matches = re.search(r"^(.+), ?(.+)$", name)
# if matches:
# name = matches.group(2) + " " + matches.group(1)
# print(f"hello, {name}")
# ## However, code above does not consider further spaces from a user
# ## i.e. if a user were to type "Malan, David"
# ## we can simple approach this by changing the '?' from above to "*"
# import re
# name = input("What's your name? ").strip()
# matches = re.search(r"^(.+), *(.+)$", name)
# if matches:
# name = matches.group(2) + " " + matches.group(1)
# print(f"hello, {name}")
## How to combine operations/code above
## Using ':=' walrus operator
import re
name = input("What's your name? ").strip()
if matches := re.search(r"^(.+), *(.+)$", name):
name = matches.group(2) + " " + matches.group(1)
print(f"hello, {name}")