Python is a modern and powerful programming language. Therefore, we can do one thing in many different ways. It's similar to format a string, a very common task in coding, we have even 4 different methods to do it:
- Classic Style String Formatting
- Modern Style String Formatting
- Latest Style String Formatting
- Standard Style String Formatting
In this post, I would like to introduce every style of string formatting in Python, do a simple example for each one. So we can understand what is it and how it works.
At the end of this post, we cover almost all of the string formatting styles in Python and be ready to select the best one for your next project.
1. Classic Style String Formatting
I called it a classic style. Because it’s quite simple and is the first method ever to format a string in Python. Furthermore, its syntax is similar to C-Style, where we use a %
operator to receive arguments.
Here is a simple example:
>>> my_name = "Mark"
>>> "My name is, %s" % my_name
'My name is, Mark'
We can understand that the %
operator here is a signal that we will substitute something here. And the character s
after it lets you know that the substitution should be a string.
After a string formatting, we can see another %
operator, it lets Python interpreter knows that it will use the value of arguments here to fill in the first one. In this case, it will substitute the value of my_name
to the string formatting.
In case the string is more complicated, we can pass multiple variables to it by using a tuple after the operator.
>>> name = "Mark"
>>> age = 25
>>> "Hello, my name is %s, I'm %s" % (name, age)
"Hello, my name is Mark, I'm 25"
Things look the same as the first example, except the number of variables that we used after the operator.
One of the disadvantages of this way is we have to take care of the order of variable in the tuple to get the right output. It will become overwhelmed soon when your code growing up and you use many variables in your string.
We can avoid this problem by using a dictionary instead of a tuple. In order to use a dictionary as an argument, we have to update the format of string a little bit as below
>>> "Hello, my name is %(name)s, I'm %(age)d" % {"name": "Bob", "age": 25}
"Hello, my name is Bob, I'm 25"
As you can see, we have to use the keys of the dictionary in the string. In that case, Python will pick exactly value from key-value pairs to substitute.
Until here this method looks good, we can format a string, can use multiples variables here. So why do we need other methods?
When Python became more and more popular, there are many more developer's comments about the lack of flexibility of this method as well. So Python Foundation heard their voice and decide to make a change here. So in the PEP 3101, Python supports another string formatting method, they called Advanced String Formatting, I called Modern Style String Formatting.
Because of the length of the original post, It will be great to have you visit the original post Anatomy of String Formatting in Python on Python Geeks for the 3 remaining String Formatting Method.