Formatting a regular string which could be a f-string
1647 if figfmt == "svg":
1648 return figbytes.decode()
1649 data_uri = _b64encode(figbytes).decode()
1650 embed_string = '<img src="data:image/{};base64,{}" />'.format(figfmt, data_uri)1651 return embed_string
Formatting a regular string which could be a f-string
1640 return figbytes.decode()
1641 data_uri = _b64encode(figbytes).decode()
1642 embed_string.join(
1643 '<img src="data:image/{};base64,{}" />'.format(figfmt, data_uri)1644 )
1645 else:
1646 figbytes = figfiles.getvalue()
Formatting a regular string which could be a f-string
838 if compounded:
839 metrics["Cumulative Return %"] = (_stats.comp(df) * pct).map("{:,.2f}".format)
840 else:
841 metrics["Total Return %"] = (df.sum() * pct).map("{:,.2f}".format) 842
843 metrics["CAGR﹪%"] = _stats.cagr(df, rf, compounded) * pct
844
Formatting a regular string which could be a f-string
836 metrics["~"] = blank
837
838 if compounded:
839 metrics["Cumulative Return %"] = (_stats.comp(df) * pct).map("{:,.2f}".format) 840 else:
841 metrics["Total Return %"] = (df.sum() * pct).map("{:,.2f}".format)
842
Formatting a regular string which could be a f-string
581 for ptf, dd_info in dd_info_dict.items():
582 iDisplay(
583 iHTML(
584 '<h4 style="margin-bottom:20px">%s - Worst 5 Drawdowns</h4>' 585 % ptf
586 )
587 )
Formatting a regular string which could be a f-string
150 for i in reversed(range(num_cols + 1, num_cols + 3)):
151 str_td = "<td></td>" * i
152 tpl = tpl.replace(
153 f"<tr>{str_td}</tr>", '<tr><td colspan="{}"><hr></td></tr>'.format(i) 154 )
155
156 tpl = tpl.replace(
Formatting a regular string which could be a f-string
1211 if x >= 1e3:
1212 res = "%1.1fK%%" % (x * 1e-3)
1213 return res.replace(".0K%", "K%")
1214 res = "%1.0f%%" % x1215 return res.replace(".0%", "%")
Formatting a regular string which could be a f-string
1209 res = "%1.1fM%%" % (x * 1e-6)
1210 return res.replace(".0M%", "M%")
1211 if x >= 1e3:
1212 res = "%1.1fK%%" % (x * 1e-3)1213 return res.replace(".0K%", "K%")
1214 res = "%1.0f%%" % x
1215 return res.replace(".0%", "%")
Formatting a regular string which could be a f-string
1206 res = "%1.1fB%%" % (x * 1e-9)
1207 return res.replace(".0B%", "B%")
1208 if x >= 1e6:
1209 res = "%1.1fM%%" % (x * 1e-6)1210 return res.replace(".0M%", "M%")
1211 if x >= 1e3:
1212 res = "%1.1fK%%" % (x * 1e-3)
Formatting a regular string which could be a f-string
1203 res = "%1.1fT%%" % (x * 1e-12)
1204 return res.replace(".0T%", "T%")
1205 if x >= 1e9:
1206 res = "%1.1fB%%" % (x * 1e-9)1207 return res.replace(".0B%", "B%")
1208 if x >= 1e6:
1209 res = "%1.1fM%%" % (x * 1e-6)
Formatting a regular string which could be a f-string
1200def format_pct_axis(x, _):
1201 x *= 100 # lambda x, loc: "{:,}%".format(int(x * 100))
1202 if x >= 1e12:
1203 res = "%1.1fT%%" % (x * 1e-12)1204 return res.replace(".0T%", "T%")
1205 if x >= 1e9:
1206 res = "%1.1fB%%" % (x * 1e-9)
Formatting a regular string which could be a f-string
1193 if x >= 1e3:
1194 res = "$%1.0fK" % (x * 1e-3)
1195 return res.replace(".0K", "K")
1196 res = "$%1.0f" % x1197 return res.replace(".0", "")
1198
1199
Formatting a regular string which could be a f-string
1191 res = "$%1.1fM" % (x * 1e-6)
1192 return res.replace(".0M", "M")
1193 if x >= 1e3:
1194 res = "$%1.0fK" % (x * 1e-3)1195 return res.replace(".0K", "K")
1196 res = "$%1.0f" % x
1197 return res.replace(".0", "")
Formatting a regular string which could be a f-string
1188 res = "$%1.1fB" % (x * 1e-9)
1189 return res.replace(".0B", "B")
1190 if x >= 1e6:
1191 res = "$%1.1fM" % (x * 1e-6)1192 return res.replace(".0M", "M")
1193 if x >= 1e3:
1194 res = "$%1.0fK" % (x * 1e-3)
Formatting a regular string which could be a f-string
1185 res = "$%1.1fT" % (x * 1e-12)
1186 return res.replace(".0T", "T")
1187 if x >= 1e9:
1188 res = "$%1.1fB" % (x * 1e-9)1189 return res.replace(".0B", "B")
1190 if x >= 1e6:
1191 res = "$%1.1fM" % (x * 1e-6)
Formatting a regular string which could be a f-string
1182
1183def format_cur_axis(x, _):
1184 if x >= 1e12:
1185 res = "$%1.1fT" % (x * 1e-12)1186 return res.replace(".0T", "T")
1187 if x >= 1e9:
1188 res = "$%1.1fB" % (x * 1e-9)
Formatting a regular string which could be a f-string
1052 )
1053
1054 ax.yaxis.set_major_formatter(
1055 _plt.FuncFormatter(lambda x, loc: "{:,}%".format(int(x * 100)))1056 )
1057
1058 if ylabel:
Formatting a regular string which could be a f-string
1027
1028 if subtitle:
1029 ax.set_title(
1030 "%s - %s \n"1031 % (
1032 returns.index.date[:1][0].strftime("%e %b '%y"),
1033 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
904 )
905 if subtitle:
906 ax.set_title(
907 "%s - %s \n" 908 % (
909 returns.index.date[:1][0].strftime("%e %b '%y"),
910 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
749
750 if subtitle:
751 ax.set_title(
752 "%s - %s \n" 753 % (
754 returns.index.date[:1][0].strftime("%e %b '%y"),
755 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
661
662 if subtitle:
663 ax.set_title(
664 "%s - %s \n" 665 % (
666 df.index.date[:1][0].strftime("%e %b '%y"),
667 df.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
547
548 # _plt.setp(x.get_legend().get_texts(), fontsize=11)
549 ax.xaxis.set_major_formatter(
550 _plt.FuncFormatter(lambda x, loc: "{:,}%".format(int(x * 100))) 551 )
552
553 # Removed static lines for clarity
Formatting a regular string which could be a f-string
445
446 if subtitle:
447 ax.set_title(
448 "%s - %s \n" 449 % (
450 returns.index.date[:1][0].strftime("%Y"),
451 returns.index.date[-1:][0].strftime("%Y"),
Formatting a regular string which could be a f-string
301
302 if subtitle:
303 ax.set_title(
304 "%s - %s \n" 305 % (
306 returns.index.date[:1][0].strftime("%e %b '%y"),
307 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
146
147 if subtitle:
148 ax.set_title(
149 "%s - %s \n" 150 % (
151 df.index.date[:1][0].strftime("%Y"),
152 df.index.date[-1:][0].strftime("%Y"),
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} ¤"