Formatting a regular string which could be a f-string
1805 print("==================================")
1806 print("self._vars={}".format(self._vars))
1807 print("self.dimension={}".format(self.dimension))
1808 print("self._vars_openpmd_files={}".format(self._vars_openpmd_files))1809 print("==================================")
1810
1811 if var_name not in self:
Formatting a regular string which could be a f-string
1804
1805 print("==================================")
1806 print("self._vars={}".format(self._vars))
1807 print("self.dimension={}".format(self.dimension))1808 print("self._vars_openpmd_files={}".format(self._vars_openpmd_files))
1809 print("==================================")
1810
Formatting a regular string which could be a f-string
1803 var_name = str(key)
1804
1805 print("==================================")
1806 print("self._vars={}".format(self._vars))1807 print("self.dimension={}".format(self.dimension))
1808 print("self._vars_openpmd_files={}".format(self._vars_openpmd_files))
1809 print("==================================")
Formatting a regular string which could be a f-string
1653 for f in allfiles:
1654 print("f={}".format(f))
1655 filename = os.path.split(f)[1]
1656 print("filename={}".format(filename))1657 matched_h5 = rx_h5.match(filename)
1658 matched_ascii = rx_ascii.match(filename)
1659 # OpenPMD files are always 3D, so we ignore them when the dimension
Formatting a regular string which could be a f-string
1651 # one of our regular expressions.
1652
1653 for f in allfiles:
1654 print("f={}".format(f))1655 filename = os.path.split(f)[1]
1656 print("filename={}".format(filename))
1657 matched_h5 = rx_h5.match(filename)
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} ¤"