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 formatting purposes for declaring, accessing, and updating the string variables. 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 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 an essential part of this article. It is not hard to format strings in python as we can use a format method in Python3 for formatting. We will 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 a 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 in the placeholder position. Curly braces represent the placeholder position. Since we pass only one parameter inside the format function, it is 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 assign 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, the placeholders are 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. 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 substitute more than one variable. Values will replace the placeholders in order. For example, if we want to substitute two values in a string, we must 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 assign the string to a variable, whereas in the second case, we directly use 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. 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. We must understand that values in the format function have tuples, meaning 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 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. Still, in the third case, we used a keyword, which means a variable whose value we assign inside the format function. We are not only using an index, so 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 essential 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 have discussed the basic concept and how to format strings in python with code implementation, respectively. You may also look at the following articles to learn more –
- Different Editors of Python
- Fibonacci Series in Python
- Reverse Number in Python
- Destructor in Python
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses