Python offers a variety of methods for string formatting. In this post, we will review three methods for formatting strings in python. Specifically, we will discuss %-formatting, str.format() and formatting strings with f-strings.
Let’s get started!
Formatting Strings with %
First, we will consider ‘%" formatting. Consider two variables that store a name and email address. We can write a function that takes the name and email and prints out a customized message using ‘%’ formatting:
def get_message_pct(name, email):
print("%s's email is %s."%(name, email))
We can call this function with values for name and email and get the following output:
get_message_pct('John', '[email protected]')

While this example is simple enough, as the number of parameters increases it becomes less readable. For example, consider a function that also takes age, height, and weight:
def get_message_pct(name, email, age, height, weight):
print("%s's email is %s. He is %s years old, %s feet tall, and weighs %s."%(name, email, age, height, weight))
get_message_pct('John', '[email protected]', 50, "6", '180 pounds')

While this works just fine, we see that the code isn’t very readable. Now let’s discuss a newer alternative to string formatting, str.format().
Formatting Strings with str.format()
Another way to format string is using str.format(). We can define another function that performs the same task as above:
def get_message_format(name, email, age, height, weight):
print("{}'s email is {}. He is {} years old, {} feet tall, and weighs {}.".format(name, email, age, height, weight))
get_message_format('John', '[email protected]', 50, "6", '180 pounds')

We can also use ‘**’ to unpack a dictionary in the str.format() method:
jake_info = {'name': 'Jake', 'email': '[email protected]', 'age':30, 'height':"6", 'weight':'170 pounds'}
get_message_format(**jake_info)

While this method is much better than %-formatting in terms of readability, it is still not as concise as we’d like. Finally, let’s discuss the newest method for formatting strings in python, f-strings.
Formatting Strings with f-strings
We can define another function that performs the same task as above using f-strings with the following code:
def get_message_fstrings(name, email, age, height, weight):
print(f"{name}'s email is {email}. He is {age} years old, {height} feet tall, and weighs {weight}.")
get_message_fstrings('Sarah', '[email protected]', 20, '5', '120 pounds')

As we can see the code is more concise and easier to read. Using f-strings is the more preferred method of string formatting in python since it provides significantly simpler syntax. I’ll stop here but I encourage you to play around with the code yourself. If you are interested in learning more about string formatting and f-strings, check out Joanna Jablonski’s blog post Python 3’s f-strings: An Improved String Formatting Syntax (Guide).
CONCLUSIONS
To summarize, in this post we discussed string formatting in python. First, we showed how to format strings using %-formatting. We then went over the more readable and less verbose method using str.format(). Finally, we discussed the newest method for formatting strings using f-strings. If you are interested learning more of the basics of python Programming, data manipulation with Pandas, and machine learning in python check out _Python for Data Science and Machine Learning: Python Programming, Pandas and Scikit-learn Tutorials for Beginners._ I hope you found this post useful/interesting. The code from this post is available on GitHub. Thank you for reading!