Formatting a regular string which could be a f-string
1225 if x >= 1e3:
1226 res = "%1.1fK%%" % (x * 1e-3)
1227 return res.replace(".0K%", "K%")
1228 res = "%1.0f%%" % x1229 return res.replace(".0%", "%")
Formatting a regular string which could be a f-string
1223 res = "%1.1fM%%" % (x * 1e-6)
1224 return res.replace(".0M%", "M%")
1225 if x >= 1e3:
1226 res = "%1.1fK%%" % (x * 1e-3)1227 return res.replace(".0K%", "K%")
1228 res = "%1.0f%%" % x
1229 return res.replace(".0%", "%")
Formatting a regular string which could be a f-string
1220 res = "%1.1fB%%" % (x * 1e-9)
1221 return res.replace(".0B%", "B%")
1222 if x >= 1e6:
1223 res = "%1.1fM%%" % (x * 1e-6)1224 return res.replace(".0M%", "M%")
1225 if x >= 1e3:
1226 res = "%1.1fK%%" % (x * 1e-3)
Formatting a regular string which could be a f-string
1217 res = "%1.1fT%%" % (x * 1e-12)
1218 return res.replace(".0T%", "T%")
1219 if x >= 1e9:
1220 res = "%1.1fB%%" % (x * 1e-9)1221 return res.replace(".0B%", "B%")
1222 if x >= 1e6:
1223 res = "%1.1fM%%" % (x * 1e-6)
Formatting a regular string which could be a f-string
1214def format_pct_axis(x, _):
1215 x *= 100 # lambda x, loc: "{:,}%".format(int(x * 100))
1216 if x >= 1e12:
1217 res = "%1.1fT%%" % (x * 1e-12)1218 return res.replace(".0T%", "T%")
1219 if x >= 1e9:
1220 res = "%1.1fB%%" % (x * 1e-9)
Formatting a regular string which could be a f-string
1207 if x >= 1e3:
1208 res = "$%1.0fK" % (x * 1e-3)
1209 return res.replace(".0K", "K")
1210 res = "$%1.0f" % x1211 return res.replace(".0", "")
1212
1213
Formatting a regular string which could be a f-string
1205 res = "$%1.1fM" % (x * 1e-6)
1206 return res.replace(".0M", "M")
1207 if x >= 1e3:
1208 res = "$%1.0fK" % (x * 1e-3)1209 return res.replace(".0K", "K")
1210 res = "$%1.0f" % x
1211 return res.replace(".0", "")
Formatting a regular string which could be a f-string
1202 res = "$%1.1fB" % (x * 1e-9)
1203 return res.replace(".0B", "B")
1204 if x >= 1e6:
1205 res = "$%1.1fM" % (x * 1e-6)1206 return res.replace(".0M", "M")
1207 if x >= 1e3:
1208 res = "$%1.0fK" % (x * 1e-3)
Formatting a regular string which could be a f-string
1199 res = "$%1.1fT" % (x * 1e-12)
1200 return res.replace(".0T", "T")
1201 if x >= 1e9:
1202 res = "$%1.1fB" % (x * 1e-9)1203 return res.replace(".0B", "B")
1204 if x >= 1e6:
1205 res = "$%1.1fM" % (x * 1e-6)
Formatting a regular string which could be a f-string
1196
1197def format_cur_axis(x, _):
1198 if x >= 1e12:
1199 res = "$%1.1fT" % (x * 1e-12)1200 return res.replace(".0T", "T")
1201 if x >= 1e9:
1202 res = "$%1.1fB" % (x * 1e-9)
Formatting a regular string which could be a f-string
1065 )
1066
1067 ax.yaxis.set_major_formatter(
1068 _plt.FuncFormatter(lambda x, loc: "{:,}%".format(int(x * 100)))1069 )
1070
1071 if ylabel:
Formatting a regular string which could be a f-string
1040
1041 if subtitle:
1042 ax.set_title(
1043 "%s - %s \n"1044 % (
1045 returns.index.date[:1][0].strftime("%e %b '%y"),
1046 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
916 )
917 if subtitle:
918 ax.set_title(
919 "%s - %s \n" 920 % (
921 returns.index.date[:1][0].strftime("%e %b '%y"),
922 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
760
761 if subtitle:
762 ax.set_title(
763 "%s - %s \n" 764 % (
765 returns.index.date[:1][0].strftime("%e %b '%y"),
766 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
671
672 if subtitle:
673 ax.set_title(
674 "%s - %s \n" 675 % (
676 df.index.date[:1][0].strftime("%e %b '%y"),
677 df.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
556
557 # _plt.setp(x.get_legend().get_texts(), fontsize=11)
558 ax.xaxis.set_major_formatter(
559 _plt.FuncFormatter(lambda x, loc: "{:,}%".format(int(x * 100))) 560 )
561
562 # Removed static lines for clarity
Formatting a regular string which could be a f-string
454
455 if subtitle:
456 ax.set_title(
457 "%s - %s \n" 458 % (
459 returns.index.date[:1][0].strftime("%Y"),
460 returns.index.date[-1:][0].strftime("%Y"),
Formatting a regular string which could be a f-string
309
310 if subtitle:
311 ax.set_title(
312 "%s - %s \n" 313 % (
314 returns.index.date[:1][0].strftime("%e %b '%y"),
315 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
153
154 if subtitle:
155 ax.set_title(
156 "%s - %s \n" 157 % (
158 df.index.date[:1][0].strftime("%Y"),
159 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} ¤"