Formatting a regular string which could be a f-string
23 self.hostname = self.DEFAULT_HOSTNAME
24 if self.pa_token:
25 log.info('Using Personal Access token.')
26 self.headers.update({'Private-Token': "{}".format(self.pa_token)})27 self.api_base = 'https://{}/api/v4'.format(self.hostname)
28 self.set_repo(repo)
29 self.repo_id = self.repo.replace('/', '%2F')
Formatting a regular string which could be a f-string
24 if self.pa_token:
25 log.info('Using Personal Access token.')
26 self.headers.update({'Private-Token': "{}".format(self.pa_token)})
27 self.api_base = 'https://{}/api/v4'.format(self.hostname)28 self.set_repo(repo)
29 self.repo_id = self.repo.replace('/', '%2F')
30
Formatting a regular string which could be a f-string
29 self.repo_id = self.repo.replace('/', '%2F')
30
31 def repo_query(self, uri):
32 url = '{}/projects/{}/repository{}'.format(self.api_base, self.repo_id, uri)33 return self.get(url)
34
35 def get_latest(self, pre_ok=False, major=None):
Formatting a regular string which could be a f-string
45 if not version:
46 continue
47 if ret and tag_date + timedelta(days=365) < ret['tag_date']:
48 log.info('The version {} is newer, but is too old!'.format(version))49 break
50 if not ret or ret and version > ret['version']:
51 log.info("Setting version as current selection: {}.".format(version))
Formatting a regular string which could be a f-string
48 log.info('The version {} is newer, but is too old!'.format(version))
49 break
50 if not ret or ret and version > ret['version']:
51 log.info("Setting version as current selection: {}.".format(version))52 ret = t
53 ret['tag_name'] = tag
54 ret['tag_date'] = tag_date
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} ¤"