Formatting a regular string which could be a f-string
1674 if figfmt == "svg":
1675 return figbytes.decode()
1676 data_uri = _b64encode(figbytes).decode()
1677 embed_string = '<img src="data:image/{};base64,{}" />'.format(figfmt, data_uri)1678 return embed_string
Formatting a regular string which could be a f-string
1667 return figbytes.decode()
1668 data_uri = _b64encode(figbytes).decode()
1669 embed_string.join(
1670 '<img src="data:image/{};base64,{}" />'.format(figfmt, data_uri)1671 )
1672 else:
1673 figbytes = figfiles.getvalue()
Formatting a regular string which could be a f-string
823 if compounded:
824 metrics["Cumulative Return %"] = (_stats.comp(df) * pct).map("{:,.2f}".format)
825 else:
826 metrics["Total Return %"] = (df.sum() * pct).map("{:,.2f}".format) 827
828 metrics["CAGR﹪%"] = _stats.cagr(df, rf, compounded) * pct
829
Formatting a regular string which could be a f-string
821 metrics["~"] = blank
822
823 if compounded:
824 metrics["Cumulative Return %"] = (_stats.comp(df) * pct).map("{:,.2f}".format) 825 else:
826 metrics["Total Return %"] = (df.sum() * pct).map("{:,.2f}".format)
827
Formatting a regular string which could be a f-string
573 for ptf, dd_info in dd_info_dict.items():
574 iDisplay(
575 iHTML(
576 '<h4 style="margin-bottom:20px">%s - Worst 5 Drawdowns</h4>' 577 % ptf
578 )
579 )
Formatting a regular string which could be a f-string
142 for i in reversed(range(num_cols + 1, num_cols + 3)):
143 str_td = "<td></td>" * i
144 tpl = tpl.replace(
145 f"<tr>{str_td}</tr>", '<tr><td colspan="{}"><hr></td></tr>'.format(i) 146 )
147
148 tpl = tpl.replace(
Description
f-strings are the fastest way to format strings as compared to the following methods:
- using format specifiers
%
- using
format()
- using
str.join
- using
+
operator to concatinate string - using
Template.substitute
Bad practice
Some less preferred ways to format strings are the following:
from string import Template
menu = ('eggs', 'spam', 42.4)
old_order = "%s and %s: %.2f ¤" % menu # [consider-using-f-string]
beginner_order = menu[0] + " and " + menu[1] + ": " + str(menu[2]) + " ¤"
joined_order = " and ".join(menu[:2])
format_order = "{} and {}: {:0.2f} ¤".format(menu[0], menu[1], menu[2])
named_format_order = "{eggs} and {spam}: {price:0.2f} ¤".format(eggs=menu[0], spam=menu[1], price=menu[2])
template_order = Template('$eggs and $spam: $price ¤').substitute(eggs=menu[0], spam=menu[1], price=menu[2])
Recommended
Consider using f-strings as shown below:
menu = ('eggs', 'spam', 42.4)
f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"