binary = 0b10111011
hexadecimal = 0xC5F
print("Binary is %d, hex is %d" % (binary, hexadecimal))Binary is 187, hex is 3167
str.format% operator
Binary is 187, hex is 3167
my_var = 1.23
keyvalue the float is coerced to a string--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 4 1 key = "my_var" 2 value = 1.234 ----> 4 formatted = "%-10s = %.2f" % (value, key) 5 print(formatted) TypeError: must be real number, not str
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 4 1 key = "my_var" 2 value = 1.234 ----> 4 formatted = "%.2f = %-10s" % (key, value) 5 print(formatted) TypeError: must be real number, not str
#0: avocados = 1.25
#1: bananas = 2.50
#2: cherries = 15.00
#1: Avocados = 1
#2: Bananas = 2
#3: Cherries = 15
Max loves food. See Max cook.
title as a method call on one use of name but not the secondbrad loves food. See brad cook.
% does resolve this by allowing dictionary key-value substitution
%(key)specifierkey = "my_var"
value = 1.234
first_method = "%-10s = %.2f" % (key, value)
second_method = "%(key)-10s = %(value).2f" % {
"key": key,
"value": value,
}
reordered_second_method = "%(key)-10s = %(value).2f" % {
"value": value,
"key": key,
}
assert first_method == second_method == reordered_second_methodBrad loves food. See Brad cook.
Today's soup is lentil.
Today's soup is lentil, buy one get two kumamato oysters, and our special entree is schnitzel
format Built-in function and str.formatformat built-in
^ centres text, injects thousands separators1,234.57
* my string *
str.format method{} rather than %formatmy_value = 1.234
: to precede format specifiers
format built-in function__format__ dunder method{{
12.50%
1.23 replaces {}
formatAlice loves food. See Alice cook
format is still verbose when the value needs to be editedrepr stringsFirst letter is 'k'
menu = {
"soup": "lentil",
"oyster": "kumamato",
"special": "schnitzel",
}
old_template = "Today's soup is %(soup)s, buy one get two %(oyster)s oysters, and our special entree is %(special)s"
old_formatted = old_template % menu
new_template = "Today's soup is {soup}, buy one get two {oyster} oysters, and our special entree is {special}"
new_formatted = new_template.format(**menu)
assert old_formatted == new_formattedstr.formatformat and it’s resultant mini-language are more useful to know from the perspective of how they informed f-stringsf
br: after the variable name e.g. {0:.2f} becomes {value:.2f}format methodf_string = f"{key:<10} = {value:.2f}"
c_tuple = "%-10s = %.2f" % (key, value)
c_dict = "%(key)-10s = %(value).2f" % {"key": key, "value": value}
str_args = "{:<10} = {:.2f}".format(key, value)
str_kw = "{key:<10} = {value:.2f}".format(key=key, value=value)
assert c_tuple == c_dict == f_string
assert f_string == str_args == str_kw# old style format method vs f-string
pantry = [
("avocados", 1.25),
("bananas", 2.5),
("cherries", 15),
]
for i, (item, count) in enumerate(pantry):
format_method_style = "#{}: {:<10s} = {}".format(i + 1, item.title(), round(count))
f_string_style = f"#{i + 1}: {item.title():<10s} = {round(count)}"
assert format_method_style == f_string_style#1: Avocados 1
#2: Bananas 2
#3: Cherries 15
% operator to format
format built-in function, and the associated str.format method introduces useful concepts for formatting
format mini-language provides the foundation for F-strings