Skip to content

How to Use Backwards Compatible String Formatting#

F-Strings Sometimes Are Not Supported#

Imagine that you want this printed in the terminal:

% I want RED color

Now that Colorist is designed to support Python 3.10 and later versions, it's often easier and more readable to use f-strings to add color to your text:

Python
1
2
3
from colorist import Color

print(f"I want {Color.RED}RED{Color.OFF} color")

But as f-strings were introduced in Python 3.6 and therefore aren't supported in earlier versions, what are the alternatives?

Backwards Compatible Alternatives#

Instead, you can use string formatting with str.format() or concatenation with +. All variations yield the same result as before:

% I want RED color

String Formatting#

Here's how you can use string formatting with str.format():

Python
1
2
3
from colorist import Color

print("I want {0}RED{1} color".format(Color.RED, Color.OFF))

String Concatenation#

Here's how you can use string concatenation with +:

Python
1
2
3
from colorist import Color

print("I want " + Color.RED + "RED" + Color.OFF + " color")