Introduction to String Formatting in Python
String formatting can be defined as the method used for formatting the string present or provided as input in the program, which formats the characters when executed. This function is employed for declaring, accessing and updating the string variables for formatting purposes. In Python, this function is attained by a particular method, which can be implemented using the syntax ‘{string variable}.format(value)’. Here, ‘value’ represents the data in any format like numbers, characters, or floating points. The different types of string formatting applied in python programming are single formatter, multiple formatter, formatter with positional and keyword arguments, and index error.
How to Format String in Python?
This is the most important part of this article which is an answer to the above question. It is not that hard to format string in python as we can use a format method in Python3 for formatting. The function is easy to use. We will take a look at the syntax of this function.
Syntax:
{string variable}.format(value)
So, we can put any string, number characters, and even other variables in place of the value. This method returns a formatted string.
There are few ways to do string formatting in python. We will look into this one by one.
1. Single Formatter
This means we pass only one parameter inside the format function, which places the value passed as a parameter in the placeholder position. Curly braces represent the placeholder position. Since we are passing only one parameter inside the format function, it is called a single formatter. To understand better, we will use Jupyter notebook to look at the output.
Code:
#program to demonstrate single Formatter in Python3
#format option for value stored in a variable
str = "Rahul is a nice {}"
print(str.format("boy"))
#format option for simple string
print("Who is inside my {}".format("house"))
So, if we look at the code, we have used two cases; in the first case, we are assigning the string to a variable, whereas in the second case, we are directly using the format function on the string.

4.8 (13,663 ratings)
View Course
Output:
As you can see in both cases, we get the placeholders replaced by the values passed as a parameter with the format function.
2. Multiple Formatters
In this case, the format method will have more than one parameter in it. This type of formatting is used when we want to do more than one variable substitution in the existing string. We will have to place extra placeholders in the string if we want to do more than one variable substitution. Values will replace the placeholders in order. For example, if we want to substitute two values in a string, then we need to place two placeholders and pass two arguments inside the format function. Let us see the code of an example.
Code:
#program to demonstrate Multiple Formatter in Python3
#format option for value stored in a variable
str = "Rahul is a nice {} and he is {}"
print(str.format("boy","young"))
#format option for simple string
print("This is {} and lots of {}".format("great","fun"))
The above code has two cases; in the first case, we are assigning the string to a variable, whereas in the second case, we are directly using the format function on the string.
Output:
As you can see in both cases, we get both the placeholders replaced by the values passed as a parameter with the format function, and the values are substituted in order which is very important to remember.
3. Formatters with Positional and Keyword Arguments
This is an interesting way of formatting using the string formatter. To understand this, we will have to understand that values placed as parameters inside the format function have tuple as their data type, which means they can be individually called using their index number starting from zero. These index numbers can be passed in the original string’s placeholders, and based on the index number, the values will be substituted in the placeholders. We will use a sample code for better understanding. Let us look at the code.
Code:
# formatters with positional key arguments.
# Positional arguments placed in order
print("{0} likes {1}!!".format("Ram","Radha"))
# Reverse the index numbers
print("{1} likes {0}!!".format("Ram","Radha"))
# Keyword arguments along with index
print("{name} likes {0}!!".format("Radha",name ="Ram"))
If you look at the above code, we can see we have used three cases; in the first two cases, we have used index numbers in placeholders. The only difference between the first two cases is the reverse order of index numbers, but in the third case, we have actually used a keyword which basically means a variable whose value we assign inside the format function. We are not only using an index, so you can see the index is zero as the first placeholder is substituted using the value of the variable passed inside the format function.
Output:
The screenshot output shows us the different cases where we can use this type of string formatting.
4. Index Error
It is important to discuss this as we should remember that there is something we get as an index error when the number of placeholders passed in a string does not match with the number of parameters inside the format function. For example, we would get an index error if we have three placeholders for two parameters in the format method.
Code:
str = "Rahul is a nice {} and he is {},{}"
print(str.format("boy","young"))
As you can see, we have an extra placeholder.
Output:
As you can see, we get an index error with the message “tuple index out of range”.
Recommended Articles
This is a guide to String Formatting in Python. Here we discuss the basic concept and how to format string in python with code implementation, respectively. You may also look at the following articles to learn more –