Search
  • 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 = "Fastapp.in"
>>> print(f"Hello World! We love {message}")

output: Hello World! We love Fastapp.in

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:

f"{last_name:_^10}"


135 views

Recent Posts

See All

Python3.8 - The Walrus Operator

In Python 3.8, we will be able to use assignment as a part of the large expressions. It has a new syntax := Examples: 1. if-else block Python3.8 my_string = "second" if (my_var := len(my_string)) > 5:

 

©2020 by FastApp. Created with <3