-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a blog post demonstrating fstring feature introduced at Python
3.6
- Loading branch information
1 parent
a9a3978
commit a858501
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
_posts/2019-01-14-fstrings-yet-another-string-formatter-in-python.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
layout: post | ||
title: "Fstring, yet another string formatter in Python" | ||
date: "2019-01-14 20:52:03 +0530" | ||
tag: | ||
- fstring | ||
- Python | ||
- Python3.6 | ||
- FormattedStrings | ||
--- | ||
|
||
|
||
Fstring is a cool feature introduced as a part of Python 3.6. Fstring can be | ||
elaborated as "Formatted String". At the time of writing this post, Python | ||
already has more than two ways to a format string. Still, Fstring is released by | ||
giving yet another way to format a string. Fstrings can be easy to understand | ||
and simple to read. When you prefix the string with ```f```, it becomes a | ||
formatted string. All names wrapped inside the curly braces ({}) are resolved | ||
with the value of variable available in local scope. If the variable isn’t found | ||
in the local scope then it will raise a ```NameError``` exception. | ||
|
||
|
||
### Demonstration | ||
|
||
![FString demonstration]({{site.url}}/assets/images/fstring_explaination/fstring_example.gif) | ||
|
||
Below is the code used in the example | ||
|
||
```python | ||
name = "Jay" | ||
print(f"Hello {name}") | ||
``` | ||
|
||
[ TODO: Find, Are there any tool avilable to convert all a formatted strings to | ||
Fstrings?] | ||
|
||
In my view, this option of string formation is more concise and readable than | ||
all other options. I advise migrating your code to Fstrings if your Python | ||
interpreter is greater than or equal to version 3.6. You should note that | ||
existing string formation approaches are still supported and are not subject to | ||
depreciate in the near future. | ||
|
||
|
||
### Summary | ||
|
||
|
||
* Fstring can be elaborated as "Formatted String". | ||
|
||
* Variable is always resolved at local scope. If it isn't found then it will | ||
rise the ```NameError``` exception. | ||
|
||
* You can format your strings by Fstring approach only if your Python | ||
interpreter version is greater than or equal to 3.6. | ||
|
||
* Existing string formation approches are not subjected to depricate soon. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.