Formatting a regular string which could be a f-string
1234 if x >= 1e3:
1235 res = "%1.1fK%%" % (x * 1e-3)
1236 return res.replace(".0K%", "K%")
1237 res = "%1.0f%%" % x1238 return res.replace(".0%", "%")
Formatting a regular string which could be a f-string
1232 res = "%1.1fM%%" % (x * 1e-6)
1233 return res.replace(".0M%", "M%")
1234 if x >= 1e3:
1235 res = "%1.1fK%%" % (x * 1e-3)1236 return res.replace(".0K%", "K%")
1237 res = "%1.0f%%" % x
1238 return res.replace(".0%", "%")
Formatting a regular string which could be a f-string
1229 res = "%1.1fB%%" % (x * 1e-9)
1230 return res.replace(".0B%", "B%")
1231 if x >= 1e6:
1232 res = "%1.1fM%%" % (x * 1e-6)1233 return res.replace(".0M%", "M%")
1234 if x >= 1e3:
1235 res = "%1.1fK%%" % (x * 1e-3)
Formatting a regular string which could be a f-string
1226 res = "%1.1fT%%" % (x * 1e-12)
1227 return res.replace(".0T%", "T%")
1228 if x >= 1e9:
1229 res = "%1.1fB%%" % (x * 1e-9)1230 return res.replace(".0B%", "B%")
1231 if x >= 1e6:
1232 res = "%1.1fM%%" % (x * 1e-6)
Formatting a regular string which could be a f-string
1223def format_pct_axis(x, _):
1224 x *= 100 # lambda x, loc: "{:,}%".format(int(x * 100))
1225 if x >= 1e12:
1226 res = "%1.1fT%%" % (x * 1e-12)1227 return res.replace(".0T%", "T%")
1228 if x >= 1e9:
1229 res = "%1.1fB%%" % (x * 1e-9)
Formatting a regular string which could be a f-string
1216 if x >= 1e3:
1217 res = "$%1.0fK" % (x * 1e-3)
1218 return res.replace(".0K", "K")
1219 res = "$%1.0f" % x1220 return res.replace(".0", "")
1221
1222
Formatting a regular string which could be a f-string
1214 res = "$%1.1fM" % (x * 1e-6)
1215 return res.replace(".0M", "M")
1216 if x >= 1e3:
1217 res = "$%1.0fK" % (x * 1e-3)1218 return res.replace(".0K", "K")
1219 res = "$%1.0f" % x
1220 return res.replace(".0", "")
Formatting a regular string which could be a f-string
1211 res = "$%1.1fB" % (x * 1e-9)
1212 return res.replace(".0B", "B")
1213 if x >= 1e6:
1214 res = "$%1.1fM" % (x * 1e-6)1215 return res.replace(".0M", "M")
1216 if x >= 1e3:
1217 res = "$%1.0fK" % (x * 1e-3)
Formatting a regular string which could be a f-string
1208 res = "$%1.1fT" % (x * 1e-12)
1209 return res.replace(".0T", "T")
1210 if x >= 1e9:
1211 res = "$%1.1fB" % (x * 1e-9)1212 return res.replace(".0B", "B")
1213 if x >= 1e6:
1214 res = "$%1.1fM" % (x * 1e-6)
Formatting a regular string which could be a f-string
1205
1206def format_cur_axis(x, _):
1207 if x >= 1e12:
1208 res = "$%1.1fT" % (x * 1e-12)1209 return res.replace(".0T", "T")
1210 if x >= 1e9:
1211 res = "$%1.1fB" % (x * 1e-9)
Formatting a regular string which could be a f-string
1074 )
1075
1076 ax.yaxis.set_major_formatter(
1077 _plt.FuncFormatter(lambda x, loc: "{:,}%".format(int(x * 100)))1078 )
1079
1080 if ylabel:
Formatting a regular string which could be a f-string
1049
1050 if subtitle:
1051 ax.set_title(
1052 "%s - %s \n"1053 % (
1054 returns.index.date[:1][0].strftime("%e %b '%y"),
1055 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
925 )
926 if subtitle:
927 ax.set_title(
928 "%s - %s \n" 929 % (
930 returns.index.date[:1][0].strftime("%e %b '%y"),
931 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
769
770 if subtitle:
771 ax.set_title(
772 "%s - %s \n" 773 % (
774 returns.index.date[:1][0].strftime("%e %b '%y"),
775 returns.index.date[-1:][0].strftime("%e %b '%y"),
Formatting a regular string which could be a f-string
680
681 if subtitle:
682 ax.set_title(
683 "%s - %s \n" 684 % (
685 df.index.date[:1][0].strftime("%e %b '%y"),
686 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} ¤"