The Python String format Method in Python is a popular inbuilt method used to print strings with variables quickly. The variables are stored in a placeholder, encapsulated in curly braces {}. An example would be something welcome,
“Welcome to {}. Enjoy this tutorial”.format(‘H2kinfosys’), which would print “Welcome to H2kinfosys. Enjoy this tutorial”.
In this tutorial, you will learn how to print strings and variables using the format method. By the end of this tutorial, you will know.
- Syntax of the format() Method
- What are Placeholders
- How the format method works
- Using positional Arguments and Leaving Placeholders Blank
- Using positional Article and Using Indexes
- Using keyword arguments
- Using f-strings in Python 3
Syntax of the format() Method
str.format(var)
Parameters
var: These are the variables that the placeholder would replace. They could be in different data types such as int, strings, etc. The placeholders are points where the variables would be replaced, and open and close curly braces denote them.
Returns
The string replaced with the variables will be returned.
What are Placeholders
Placeholders represent points where the replacement would take place. Depending on the kind of arguments used in the format method, the placeholders could be left empty, be filled with integers or variable names. We will discuss all the scenarios in this tutorial.
How the format method works
When the python interpreter runs a code and encounters the format method, it replaces the placeholders’ arguments. The arguments could either be positional arguments (where the curly braces are empty) or keyword arguments (where the curly braces are filled with indexes such 0, 1, 2, etc. Now let’s discuss the different scenarios.
Using positional Arguments and Leaving Placeholders Blank
When using positional arguments, the variables are placed following the order of the format method’s arguments. In the example below, the user entered David as his name. Say we wish to print a ‘My name is David’ message. Let’s see how it’s done with the format method.
#print a name using format() method name = input('Please enter your name: ') string = 'My name is {}'.format(name) print(string)
Output:
My name is David
Note that you can use more than one placeholder. The interpreters replace the arguments sequentially. In the example below, three placeholders will be created as well as the three arguments. The arguments are replaced with the placeholders in their order of arrangements.
#print a name using format() method string = 'My name is {}. I am learning {} with {}'.format('David', 'Python', 'H2k Infosys') print(string)
Output:
My name is David. I am learning Python with H2k Infosys
Easy as that.
Using positional Article and Using Indexes
The position can also be tweaked by changing the index in the placeholder. Rather than leaving them empty, you can fill them with indexes of the arguments. When you use the indexing, the Python interpreter no longer follows how the arguments were arranged but the indexes used.
In the example below, notice that the arguments are written as
'Python', 'David', 'H2k Infosys'
instead of
'David', 'Python', 'H2k Infosys'.
We can, however, print the same result as the last example by passing the index on the placeholder. Let’s run with and without the placeholders left empty.
First case
#print a name using format() method string = 'My name is {}. I am learning {} with {}'.format('Python', 'David', 'H2k Infosys') print(string)
Output:
My name is Python. I am learning David with H2k Infosys
As expected, it prints my name as Python…
Now, let’s fix this by using Indexes.
#print a name using format() method string = 'My name is {1}. I am learning {0} with {2}'.format('Python', 'David', 'H2k Infosys') print(string)
Output:
My name is David. I am learning Python with H2k Infosys
As seen, it prints what we wanted. One advantage of this method is that you can reuse keywords more than once. Let’s assume we want ‘H2k Infosys’ to appear twice in the string, it is all about passing the index again. See the example below.
#print a name using format() method string = 'My name is {1}. I am learning {0} with {2}. I love {2}.'.format('Python', 'David', 'H2k Infosys') print(string)
Output:
My name is David. I am learning Python with H2k Infosys. I love H2k Infosys.
Using keyword arguments
Another method of using the format() method is by using keyword arguments. When using keyword arguments, the replacement is done based on the keyword assigned in the placeholder.
If the argument in the format method was name=’David’, Python replaces all instances of {name} with David.
See the code below.
#print a name using format() method string = 'My name is {name}. I am learning {course} with {company}. \ {name} loves {company}.'.format(name='David', course='Python', company='H2k Infosys') print(string)
Output:
My name is David. I am learning Python with H2k Infosys. David loves H2k Infosys.
Using f-strings in Python 3
One of the key updates from Python 2 to Python 3 is the addition of f-strings. Using python f-strings is an easier way of doing string formatting. It works like the format() method, however, it is much more convenient.
To use f string, the letter f is typed following by “” that contains the string. The variables or values are placed directly in the placeholder. See an example below.
name = 'David' string = f"Hi, my name is {name}!" print(string)
Output:
Hi, my name is David!
It can do more complicated string formatting in one sweep.
#print using formatted strings (f-strings) name = 'David' course = 'Python' company = 'H2k Infosys' #using f-strings string = f'My name is {name}. I am learning {course} with {company}. \ {name} loves {company}.' print(string)
Output:
My name is David. I am learning Python with H2k Infosys. David loves H2k Infosys.
In summary, you have seen how to do string formatting using the format method. When using the format method, you can either leave the placeholders blank, fill with the argument’s index or fill with the keywords of the argument. Finally, you have learned how to f-strings, which is a faster and cleaner way of string formatting. If you’ve got any questions, feel free to leave them in the comment section and I’d do my best to answer them.
One Response