Explain String Formatting in Python?

String formatting is the process of infusing things in the string dynamically and presenting the string. There are four different ways to perform string formatting:-

  • Formatting with % Operator.
  • Formatting with format() string method.
  • Formatting with string literals, called f-strings.
  • Formatting with String Template Class

So we will see the entirety of the above-mentioned ways, and we will also focus on which string formatting strategy is the best.

Example: Formatting string using % operator


print("The mangy, scrawny stray dog %s gobbled down" + "the grain-free, organic dog food." %'hurriedly')

Output:

The mangy, scrawny stray dog hurriedly gobbled down the grain-free, organic dog food.