Formatting a regular string which could be a f-string
916 hline=returns.mean(),
917 hlw=1.5,
918 ylabel=ylabel,
919 title="Rolling Sortino (%s)" % period_label, 920 fontname=fontname,
921 grayscale=grayscale,
922 lw=lw,
Formatting a regular string which could be a f-string
872 hline=returns.mean(),
873 hlw=1.5,
874 ylabel=ylabel,
875 title="Rolling Sharpe (%s)" % period_label, 876 fontname=fontname,
877 grayscale=grayscale,
878 lw=lw,
Formatting a regular string which could be a f-string
822 hline=returns.mean(),
823 hlw=1.5,
824 ylabel=ylabel,
825 title="Rolling Volatility (%s)" % period_label, 826 fontname=fontname,
827 grayscale=grayscale,
828 lw=lw,
Formatting a regular string which could be a f-string
657 resample=resample,
658 grayscale=grayscale,
659 fontname=fontname,
660 title="Distribution of %sReturns" % title, 661 figsize=figsize,
662 ylabel=ylabel,
663 subtitle=subtitle,
Formatting a regular string which could be a f-string
445 title = "Cumulative Returns" if compound else "Returns"
446 if benchmark is not None:
447 if isinstance(benchmark, str):
448 title += " vs %s (Log Scaled" % benchmark.upper() 449 else:
450 title += " vs Benchmark (Log Scaled"
451 if match_volatility:
Formatting a regular string which could be a f-string
391 title = "Cumulative Returns" if compound else "Returns"
392 if benchmark is not None:
393 if isinstance(benchmark, str):
394 title += " vs %s" % benchmark.upper() 395 else:
396 title += " vs Benchmark"
397 if match_volatility:
Formatting a regular string which could be a f-string
332 ax.plot(returns.index, returns, color=colors[1], lw=1 if grayscale else lw)
333
334 ax.set_ylabel(
335 "Value of ${:,.0f}".format(start_balance), 336 fontname=fontname,
337 fontweight="bold",
338 fontsize=12,
Formatting a regular string which could be a f-string
306 "${:,}".format(round(returns.values[-1] - returns.values[0], 2))
307 ),
308 _utils._score_str(
309 "{:,}%".format( 310 round((returns.values[-1] / returns.values[0] - 1) * 100, 2)
311 )
312 ),
Formatting a regular string which could be a f-string
303 returns.index.date[1:2][0].strftime("%e %b '%y"),
304 returns.index.date[-1:][0].strftime("%e %b '%y"),
305 _utils._score_str(
306 "${:,}".format(round(returns.values[-1] - returns.values[0], 2)) 307 ),
308 _utils._score_str(
309 "{:,}%".format(
Formatting a regular string which could be a f-string
298
299 if subtitle:
300 ax.set_title(
301 "\n%s - %s ; P&L: %s (%s) " 302 % (
303 returns.index.date[1:2][0].strftime("%e %b '%y"),
304 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
139 )
140 elif isinstance(returns, _pd.DataFrame):
141 axes[0].set_title(
142 "\n%s - %s ; " 143 % (
144 returns.index.date[:1][0].strftime("%e %b '%y"),
145 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
128 if subtitle:
129 if isinstance(returns, _pd.Series):
130 axes[0].set_title(
131 "%s - %s ; Sharpe: %.2f \n" 132 % (
133 returns.index.date[:1][0].strftime("%e %b '%y"),
134 returns.index.date[-1:][0].strftime("%e %b '%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} ¤"