String Formating & F-String syntax

Developers, throughout their development journey, would need to work with different data structures. Of the different data structures available in Python, a close look into Strings type would be considered in this lesson.

Before we dive into using the new F-string syntax introduced in Python 3.6, we’ll walk you through the old ways of formatting Strings. With that, you would obviously see why the newly improved syntax comes in handy.

Let’s have a simple code snippets to work with in order to have an idea of what is going on under the hood.

Consider the simple code below.

fruit = 'Orange'
price = 50

We need to generate this output:

The current fruit in the fridge is Orange and the price is $50

Method 1: Concatenating the output with +

A very simple approach to obtain the output is to concatenate the total output using +. This is supported in all progamming languages including Python. But the problem here is that it is really clumsy and inefficeint to use. Problem starts to arise as our application becomes larger.

Open Python Shell or any other IDE to run the code:

>>> print('The current fruit in the fridge is ' +  fruit + ' and the price is $' + str(price))
The current fruit in the fridge is Orange and the price is $50

Up here, we have successfully generated the desired result .

But if you look closely, you will notice that we had to convert the variable price (which is an Integer) to a String type using str before concatenation operation was possible.

Method 2: Using %-formatting

Another simple approach to achive the result it to use the buit-in % operator

Just like the first method, it suffers readability. You need to look pretty well where the % is postioned to have the outcome

>>> print('The current fruit in the fridge is  %s and the price is $%s' %(fruit, price))
The current fruit in the fridge is Orange and the price is $50

This code will start becoming really hard to read when we have many parameters to pass in.

Method 3: Using str.format()

str.format() is an improvement on %-formatting. It uses the Curly brackets {} to hold the position of the parameters to be passed.

Consider how the syntax is used below:

>>> print('The current fruit in the fridge is {} and the price is ${}'.format(fruit, price))
The current fruit in the fridge is Orange and the price is $50

We can see from the code up here that str.format() is much more readable than code using %-formatting . But the code will be clumsy when we have many with many parameters and longer string.

Method 4: Using New F-String Syntax Introduced in Python 3.6

Using the f-String syntax makes working with string extremely easy. We only need write our string out and input the parameters into a Curly brackets {}.

>>> print(f'The current fruit in the fridge is {fruit} and the price is ${price}')
The current fruit in the fridge is Orange and the price is $50

Observe how we put f before the parenthesis. We can also use F instead to get the result

>>> print(F'The current fruit in the fridge is {fruit} and the price is ${price}')
The current fruit in the fridge is Orange and the price is $50

F-String also supports expressions in the curly brackets {}

>>> a = 3
>>> b = 12
>>> print(F' Sum of {a} and {b} is {a+b}')
Sum of 3 and 12 is 15

Hopefully you are able to get how the new F-strings comes in handy when working with Strings.

Enjoyed this article?

Ready to elevate 🚀 your Python 🐍 skills? Check out YouTube for exclusive content. Don’t forget to subscribe to the channel to stay updated with future releases.

--

--

Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use