String Formatting in Python

Rohit Patil
5 min readJan 2, 2022

--

When I first started programming, I used to concatenate a string to add dynamic values in a string which used to look something like this:

String concatenation in Python.

This worked completely fine. But as I started to add more values, It started becoming hard to maintain. Take a look at example given below.

String concatenation with multiple variables.

So Let’s look at some of the better ways to do the string formatting in Python apart from string concatenation.

1. Using % sign and type matching placeholder

One of the old ways to format a string is to use % sign and placeholder which matched the type of variable.

In above example, I have used multi line string along with placeholder where I want to place value of variables. If you observe properly, I have used %s for strings and %d for integer value also variables in tuple that we used after % are in order of their position in the string.

2. Using .format method

Another way to format a string is to use .format method. Similar to previous method we seen, we have to use placeholder where we want variables values except we don’t have to use type matching placeholder rather there are different ways in which we can use placeholders.

  • Use index position in {}
  • Use blank placeholder with open-close curly braces
  • Use variable in {}

Let’s have a look at them one by one.

i. Use index position in {}

In given example, I am basically passing positional arguments to the format function. Then we have to use index of argument inside curly braces as placeholder in a string. This can be in any order, we just have to make sure that we are using right index while using placeholders.

This example will give same result as previous one. Observe how I have actually change the position of TEAM_NAME and OTP while passing the arguments.

ii. Use blank placeholder with open-close curly braces

Like in previous example, We are passing arguments to the format function. But instead we haven’t mentioned any index number in curly braces. In this case we have to make sure that arguments that are passed to format function are in order. Swapping position of variables will give us different result.

iii. Pass keyword argument to format function

format works just like any other function. We can also pass keyword arguments to this function and then we can use this arguments as placeholder with curly braces around it. This way we can make sure that we are using right placeholder at right place. We can also do the following in this case:

You can use any name to your keyword argument, just make sure that you’re passing right values to them.

3. F-String in Python

F-strings are my favourite ones and I used them a lot while programming in python. Instead of passing any arguments to any function we can directly use variables as placeholder surrounded by curly braces. We have to use f at start of the string in order for it to work. Let’s have a look at example given below:

Here we have used f at very start of string (before quotes) and then used actual variables that we want to use as placeholder which makes it super easy to manage and more readable.

Performance of all methods for string formatting

Okay, So we have understood the different methods in which we can use string formatting. Let’s also compare performance of all of them. To compare result I have actually added each method in a loop and it iterates it for n number of time (In our case, n = 10000000).

Hers’s an example for f-string which is iterated for N times. (You can see I was able to perform subtract operation in curly braces which is something I like the most about F-strings)

Now let’s have a look at results.

All differences shown in above image are in seconds. As you can see, F-string performed much better compared to all other methods. Whereas, format method with keyword argument took more than double the time than any other method. This may or may not affect in your project depends on what you are doing in your project but It is always best to have some knowledge about this metrics.

Conclusion

We can use different methods to format the string in python. For me, F-string method is work with which also happens to be the fastest one among all.

Let me know which one you use. Follow me on Twitter for more daily tips on Python.

--

--

Rohit Patil

👨‍💻 Former Tech Lead, Software Consultant & Backend Engineer with 4+ years of hands-on expertise 🚀