• Team FastApp

Python3.6 - What the f-string !

In Python 3.6, there is a new way to format the strings, which helps the user in faster coding, less googling, and better readable code. Which was previously a little tedious by using %-format or even by using string.format() . You haven’t forgotten about how we used to insert any variable inside the string by using the above two old methods, have you? Well let’s not go back to old methods, and now let’s see how we can use the f-string.

Hello World!!!

Its syntax is simple

>>>> f"Hello World"

Let’s see how to use variable along with this:

>>> message = ""
>>> print(f"Hello World! We love {message}")

output: Hello World! We love

In the below example, we call the function from inside the f-string and print the returned data from the function, cool right ?

>>> def checker(number):
...     return True if number == 0 else False
>>> num = 10
>>> print(f"Is {num} equal to 0? {checker(num)}")

output: Is 10 equal to 0? False

We can use f-strings even when there is a dictionary instead of just variable.

>>> user = {'first_name': 'Light', 'last_name': 'Yagami'}
>>> print(f"{user}")

output: {'first_name': 'Light', 'last_name': 'Yagami'} 

We can even take a step further by doing something like this, which will give individual

data. And please take note of the use of " and '.

>>> print(f"{user ['first_name']}")

We can even do a multiline f-stings

>>> first_name = 'Light'
>>> last_name = 'Yagami'
>>> full_name = (
...     f"My first name is {first_name} "
...     f"and last name is {last_name}."
... )
>>> print(full_name)

output: My first name is Light and last name is Yagami.

Note: we need to prepend "f" for each new line :)

Let's see some paddings examples:

This will get total of 4 padding spaces at the start followed by the last_name which is 6 characters.

So total comes to 10.

>>> last_name = 'Yagami'
>>> f"{last_name:>10}"

output: '    Yagami'

The following line of code will give left aligned text and trailing padding spaces so total character count sums to 10 (6+4).

>>> f"{last_name:10}"

output: 'Yagami    '

Now let us get the text in centre.

>>> f"{last_name:^10}"

output: '  Yagami  '

Truncating some characters of a long string.

>>> f"{last_name:.3}"
output: 'Yag'

we can also try combining padding & truncating of character in one shot.

>>> f"{last_name:10.3}"
output: 'Yag       '

How does f-sting behave with numbers:

>>> number = 16.67890
>>> f"{number:f}"
output: '16.678900'

We can also try padding along with numbers

>>> f"{number:10f}"
output: ' 16.678900'

the following code will add 0’s at the start and also round off the number to 2 digits only after decimal point:

>>> f"{number:08.2f}"
output: '00016.68'

Writing some complex code with variables:

>>> last_name = 'Yagami'
>>> sign = '^'
>>> width = 10
>>> f"{last_name:{sign}{width}}"
output: '  Yagami  '

Now you try the following code and comment below what you get as output:


165 views0 comments

Recent Posts

See All

This blog post aims to make django querying simple for the developers who are new in the django world. Keep reading :) What is a django query? A django query is an expression which can be used to writ

Consider, we have a StudentHistory model defined as: class StudentHistory(models.Model): first_name = models.CharField(max_length=30, blank=True) last_name = models.CharField(max_length=150, b