-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_flat_replace.py
More file actions
43 lines (32 loc) · 943 Bytes
/
b_flat_replace.py
File metadata and controls
43 lines (32 loc) · 943 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
37
38
39
40
41
42
43
""" B Flat Replace
Demoing to someone the different ways you can replace b flat ("♭") in a string
"""
import re
B_FLAT = "♭"
TEST_STRING = "here is my test ♭ string"
REPLACE_VALUE = "b"
def simple_replace():
print(TEST_STRING.replace(B_FLAT, REPLACE_VALUE))
def regex_replace():
regex = "♭"
print(re.sub(regex, REPLACE_VALUE, TEST_STRING))
def ascii_replace():
ascii_list = [ord(x) for x in TEST_STRING]
replaced_ascii_list = [ord(REPLACE_VALUE) if code == ord(B_FLAT) else code for code in ascii_list]
print("".join([chr(x) for x in replaced_ascii_list]))
print(f"Starting string: {TEST_STRING}\nSimple Replace")
simple_replace()
print("Regex Replace")
regex_replace()
print("ASCII replace")
ascii_replace()
# Results:
#
# Starting string: here is my test ♭ string
#
# Simple Replace
# here is my test b string
# Regex Replace
# here is my test b string
# ASCII replace
# here is my test b string