String Formatting in Python – A Quick Overview

String formatting, as the name suggests, refers to the multiple ways to format strings in Python. In this tutorial, we will discuss the different ways and how to use them.

What is String Formatting?

Let us say you are writing a program that prints the square of a number. In your input, you get an integer from the user, and in the output, you tell the user that the square of the integer is so and so.

For Example, if the input is 12, then you’ll need to print “The square of 12 is 144”. We cannot write a complete string like this because we have two integers to insert in the string, so we will need a way to generate this string. String formatting allows us to do that.

Note: If we do print("The square of", input, "is", result), this will print four different things, it will not generate a formatted string, so this doesn’t count as string formatting.

Types of String Formatting Techniques

Today we will be discussing three ways of formatting strings:

  1. Using the % operator