Hi everyone, In this tutorial, we’ll be learning about string formatting and what are the various ways in python by which we can format strings according to our need using suitable examples.

1. What is String Formatting?

Let’s take a scenario, We want to send emails to 5 people whose names are present in a list, the content of all the emails is  “Hi {person_name}, Welcome to Onlinetutorialspoints.” where we have to replace the person_name placeholder with person name present in the list and then send the email to their respective email address.

For 5 people we can manually send all emails by changing the name and send, But imagine if there are 1000 people, Definitely this approach fails. To solve this we have string formatting. If we can replace a variable with different values at runtime we can use a simple loop to change that variable again and again.

So, String formatting is a way to format the strings according to the user needs by replacing a placeholder with the required data.

2. Ways of String formatting in Python

Python provides several ways to format strings. Let’s take a look at each of them individually in the following sections.

2.1 Using ‘%’ Operator

If you have a prior programming language knowledge of C++ etc, then it will be familiar to you. Not to worry if you don’t know, we will discuss everything here. When there is a need for the variable(s) to be placed inside strings, we can use format specifiers inside strings that shows that this place will be reserved for a variable whose value is provided at the end inside a tuple. Let’s look at the different format specifiers in Python with an example.

Different types of format specifiers in Python

  • %d – Integer variable
  • %s – A string variable
  • %f – Floating-point variable

There are many more which you can refer to on this link. Let us use the string type format specifier by printing the above string.

person_name="Kunal Gupta"
print("Hi %s, Welcome to Onlinetutorialspoints." %(person_name,))

Output:

Hi Kunal Gupta, Welcome to Onlinetutorialspoints.

In the above example, %s signifies that this place in the string will be replaced by a string representation and whose value will be given inside a tuple at the end of the string, like in our case %(person_name,).

2.2 Python format()

In this way of string formatting, we use format() method with objects of type string. The syntax of this type takes the form

str.format(key0=value0,key1,value1,….)

where the number of values that format() method takes are at least the number of placeholders that are present in the str.

Let us see different examples using the format() method to achieve the same results with the string that we used above for a single

Hi {person_name}, Welcome to Onlinetutorialspoints.

2.2.1 Using default indexing. –

f_name="Kunal"
l_name="Gupta"

s = "Hi {0} {1}, Welcome to the Onlinetutorialspoints.".format(f_name,l_name)
print(s)

Output:

Hi Kunal Gupta, Welcome to the Onlinetutorialspoints.
Changing the order of the index in the placeholders will result in different output.

2.2.2 Custom key-value pair.

full_name = "Kunal Gupta" 
s = "Hi {full_name}, Welcome to the Onlinetutorialspoints.".format(full_name=full_name)
print(s)

Output:

Hi Kunal Gupta, Welcome to the Onlinetutorialspoints.

2.3 Python f-strings – formatted strings

It is the type of string formatting we can directly put variable expression inside the curly braces{} in a string. a character f is prefixed with the string that shows that this is a formatted string (f-string). Let us take a look at the example. This type of string formatting is available with Python version 3.6 or more.

full_name = "Kunal Gupta" 
s = f"Hi {full_name}, Welcome to the Onlinetutorialspoints."
print(s)

Output:

Hi Kunal Gupta, Welcome to the Onlinetutorialspoints.

In a nutshell, in this tutorial, we saw three different ways of formatting strings in Python i.e, by using %operator, format() method and the f-strings. Hope you liked this tutorial.

3. References

Happy Learning 🙂