Its syntax is simple
Let’s see how to use variable along with this:
In the below example, we call the function from inside the f-string and print the returned data from the function, cool right ?
We can use f-strings even when there is a dictionary instead of just variable.
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 '.
We can even do a multiline f-strings
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.
The following line of code will give left aligned text and trailing padding spaces so total character count sums to 10 (6+4).
Now let us get the text in centre.
Truncating some characters of a long string.
we can also try combining padding & truncating of character in one shot.
How does f-sting behave with numbers:
We can also try padding along with numbers
the following code will add 0’s at the start and also round off the number to 2 digits only after decimal point:
Writing some complex code with variables:
Now you try the following code and comment below what you get as output:
Outsource the best Development team now!
You know what a great tech team can do to your business. It is time to act fast and outsource the best one. But who to pick or choose? MarsDevs got your back!
We have a team of developers who can build, deploy, and manage applications for you so that you can focus on the business better. As soon as you register, our expertise becomes yours!