-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_Format_String.py
49 lines (40 loc) · 2.37 KB
/
1_Format_String.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
# Reference: https://docs.python.org/3/library/stdtypes.html#str.format
# Format the title in a graph; show the message or error; pass the statement into a function
# Positional formatting: 'text{}'.format(value)
# The format method replaces the placeholders using the values.
print('{} helps prevent side effects due to drug reactions and toxic chemicals'.format('Cysteine'))
# Cysteine helps prevent side effects due to drug reactions and toxic chemicals
str1 = '{} helps prevent side effects due to drug reactions and toxic chemicals'
amino_acid = 'Cysteine'
str1.format(amino_acid)
# 'Cysteine helps prevent side effects due to drug reactions and toxic chemicals'
# Reorder the placeholder
str2 = '{1} helps {0} side effects due to drug reactions and toxic chemicals'
amino_acid = 'Cysteine'
function = 'prevent'
str2.format(function, amino_acid)
# 'Cysteine helps prevent side effects due to drug reactions and toxic chemicals'
# Specify the name for the placeholder
str3 = '{amino_acid} helps {function} side effects due to drug reactions and toxic chemicals'
str3.format(amino_acid=amino_acid, function=function)
# 'Cysteine helps prevent side effects due to drug reactions and toxic chemicals'
str4 = {'method': 'Supervised learning', 'goal': 'find patterns'}
print('{data[method]} tries to {data[goal]} in the dataset.'.format(data=str4))
# Supervised learning tries to find patterns in the dataset.
# Format specifier
# f'{value:{width}.{precision}}'
# value -> any expression that evaluates to a number
# width -> the number of characters used in total to display, but if value needs more space than the width specifies, then the additional space is used
# precision -> the number of characters used after the decimal point
print('Only {0:.2f}% of the {1} produced worldwide is {2}!'.format(0.12890, 'protein data', 'analyzed'))
# Only 0.13% of the protein data produced worldwide is analyzed!
# :.2f -> use the float with 2 decimal places
print("Validation MAE for Random Forest Model: {:,.0f}".format(rf_val_mae))
# : -> indicate formatting follows
# , -> add thousands separator (e.g., 10,000 instead of 10000)
# .0f -> use the float with 0 decimal places == round to zero decimal places (display as an integer)
# Call the function
def multiply_two_num(m, n):
return m * n
print(f'If you mulitply 5 by 6, the result will be {multiply_two_num(5, 6)}.')
# If you mulitply 5 by 6, the result will be 30.