-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings.py
56 lines (44 loc) · 1.76 KB
/
Strings.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
def STR():
assert str(42) == "42"
assert str(True) == "True"
assert str(dict) == "<class 'dict'>"
def CHR_ORD(): # 2 inverse functions
assert chr(65) == "A"
assert ord("A") == 65
assert chr(ord(100)) == 100
def BIN_HEX_OCT(): # representations of numbers in binary, octary and hexary systems
assert bin(42) == "0b101010"
assert oct(42) == "0o52"
assert hex(42) == "0x2a"
assert int("0b101010", 2) == 42
assert int("0o52", 8) == 42 # int can reverse this process by passing the base that was used as the second argument
assert int("0x2a", 16) == 42
def FORMAT():
name = "Alice"
age = 31
formatedString = f"Name: {name}, Age: {age}"
formatedString = "Name: {}, Age: {}".format(name, age)
assert formatedString == "Name: Alice, Age: 31"
formatedString = f"Name: {name}, Age: {age:x}" #turn into hexary system
assert formatedString == "Name: Alice, Age: 1f"
formatedString = f"Name: {name}, Age: {age:b}" #turn into binary system
assert formatedString == "Name: Alice, Age: 11111"
formatedString = f"Name: {name}, Age: {age:o}" #turn into octary system
assert formatedString == "Name: Alice, Age: 1f"
#how to change it
class MyCls:
def __format__(self, formatSpecs = None):
if (formatSpecs == "x"):
return "<HEX>"
if (formatSpecs == "o"):
return "<OCT>"
if (formatSpecs == "b"):
return "<BIN>"
if (formatSpecs == "specialBase"):
return "<SPECIAL>"
return "<NORMAL>"
def INPUT_PRINT(): # pauses your program until user enters at terminal
name = input("Enter your name: ")
assert isinstance(name, str)
print(name)
print(name, file=sys.stderr) # the file parameter specifies where the messeage should be written