-1

I want to print a colored text in console. So I use this code provided here:

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

I'm hesitant about the way we can use this class. It has been said that:

To use code like this, you can do something like

print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)

or, with Python3.6+:

print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")

I'm using python 3.7, but both of these codes work for me, so I wanted to know how the usage is different based on Python version and what is that f mentioned Python3.6+ part?

Pablo
  • 465
  • 1
  • 4
  • 14
  • The first example is using string concatenation which is available in all versions of python. The second one uses f-strings which were introduced in python 3.6. They are a "nicer" way of doing the same thing. – rdas May 17 '20 at 12:58
  • 1
    BTW: you can also use `"{}Warning: No active frommets remain. Continue?{}".format(bcolors.WARNING, bcolors.ENDC)`. There is also old method with `%` - see more: https://pyformat.info/ – furas May 17 '20 at 13:12
  • I don't see what that question has to do with colored text... Your question is basically what is `f`-strings? https://stackoverflow.com/questions/43123408/f-strings-in-python-3-6 – Tomerikoo May 17 '20 at 14:47

1 Answers1

0

Technically speaking, the f-strings are faster. So you should consider using them if performance matters. See “Python f-strings: fun strings, fast strings” by Gus Goulart https://link.medium.com/l54akhN0y6

Ori Cohen
  • 185
  • 9