New ‘=’ Operator in Python3.8 f-string

Python have introduced the new = operator in f-string for self documenting the strings in Python 3.8.2 version. Now with the help of this expression we can specify names in the string to get the exact value in the strings despite the position of the variable. Now f-string can be defined as f'{expr=}' expression. We can specify any required name in place of expr.

Syntax : f'{expr=}'
Return : Return the formatted string.

Note: For older versions of Python this operator will raise the syntax error. See the below image.

Example #1 :
In this example we can see that with the help of f'{expr=}' expression, we are able to format strings in python by self documenting expressions by using = operator.



filter_none

edit
close

play_arrow

link
brightness_4
code

length = len('GeeksForGeeks')
  
# Using f'{expr =}' expression
gfg = f'The length of GeeksForGeeks is {length =}.'
  
print(gfg)

chevron_right


Output :

The length of GeeksForGeeks is length=13.

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

a, b = 5, 10
  
# Using f'{expr =}' expression
gfg = f'Value of {b = } and {a = }.'
  
print(gfg)

chevron_right


Output :

Value of b = 10 and a = 5.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.